Currency Conversion Rate
Problem statement
You are given currency conversion rates. Each row contains a source currency, a target currency, and the value of one unit of the source currency in the target currency.
A conversion may use multiple rates. A listed rate may also be used in reverse by taking its reciprocal.
Given a query [from, to], return the conversion rate from from to to, rounded and formatted with exactly two digits after the decimal point. Every judged query is connected by the supplied rates.
Function
findConversionRate(rates: String[][], query: String[]) → StringExamples
Example 1
rates = [["USD","JPY","110"],["USD","AUD","1.45"],["JPY","GBP","0.0070"]]query = ["GBP","AUD"]return = "1.88"Use the reverse of JPY -> GBP, then the reverse of USD -> JPY, then USD -> AUD: (1 / 0.0070) * (1 / 110) * 1.45 = 1.883116.... Rounded to two decimal places, the result is 1.88.
Example 2
rates = [["USD","CAD","1.30"],["CAD","EUR","0.70"]]query = ["USD","EUR"]return = "0.91"One USD is 1.30 CAD, and one CAD is 0.70 EUR, so the rate is 1.30 * 0.70 = 0.91.