You are given several exact relationships between currencies. Use these relationships to determine the conversion rate between any two currencies.
A conversion may pass through multiple intermediate currencies.
The currency conversion graph is represented by a List<String>. Each string has the format "fromCurrency,toCurrency,rate".
For example, "USD,CAD,2.0" means that one unit of USD equals 2.0 units of CAD.
CurrencyConverter
public CurrencyConverter()
public double getConversionRate(List<String> conversionGraph, String fromCurrency, String toCurrency)
conversionGraph contains the available currency relationships.toCurrency equal to one unit of fromCurrency.-1.0 if either currency is unknown or no conversion chain connects them.public List<Double> getConversionRates(List<String> conversionGraph, List<String> queries)
conversionGraph.queries has the format "fromCurrency,toCurrency".-1.0 if either currency is unknown or the currencies are not connected.A equals rate units of currency B, then one unit of B equals 1.0 / rate units of A.1.0.-1.0.n = conversionGraph.size()1 ≤ n ≤ 100,000q = queries.size()1 ≤ q ≤ 100,0001 ≤ fromCurrency.length(), toCurrency.length() ≤ 200.001 ≤ rate ≤ 1,000.0double.10-4 of the expected value are accepted. Round final answer after 4 decimal places and remove trailing zeros after decimal.getConversionRate(conversionGraph = List.of("USD,CAD,2.0", "CAD,MXN,5.0"), fromCurrency = "USD", toCurrency = "MXN")
Output: 10.0
One USD equals two CAD, and one CAD equals five MXN. Therefore, one USD equals 2.0 * 5.0 = 10.0 MXN.
getConversionRate(conversionGraph = List.of("GBP,EUR,2.0", "EUR,JPY,2.0"), fromCurrency = "JPY", toCurrency = "GBP")
Output: 0.25
One GBP equals four JPY, so one JPY equals 1.0 / 4.0 = 0.25 GBP.
getConversionRates(conversionGraph = List.of("USD,CAD,2.0", "CAD,MXN,5.0", "EUR,CHF,0.5"), queries = List.of("USD,MXN", "MXN,USD", "USD,CHF", "CAD,CAD", "XYZ,XYZ"))
Output: List.of(10.0, 0.1, -1.0, 1.0, -1.0)
USD converts to MXN through CAD, and the reverse rate is 0.1. USD and CHF are disconnected, CAD is known, and XYZ is unknown.