Can you solve in Java with a HashMap<HashMap<String, Integer>> using DFS approach?
Currency Exchange
Programming challenge description:
Given:
- A list of foreign exchange rates
- A selected currency
- A target currency
Your goal is to calculate the maximum amount of the target currency to 1 unit of the selected currency through the FX transactions. Assume all transactions are free and done immediately. If you cannot complete the exchange, return -1.0.
Example:
Input:
You will be provided a list of FX rates, a selected currency, and a target currency.
FX rates list:
USD to JPY 1 to 109
USD to GBP 1 to 0.71
GBP to JPY 1 to 155
Original currency: USD
Target currency: JPY
Output:
Calculate the maximum target currency that can be obtained.
Example:
USD to JPY -> 109
USD to GBP to JPY = 0.71 * 155 = 110.05
> 109,
so the maximum value will be 110.05
Test 1:
Test Input:
USD,GBP,0.7;USD,JPY,109;GBP,JPY,155;CAD,CNY,5.27;CAD,KRW,921
USD
CNY
Expected Output:
-1.0
Test 2:
Test Input:
USD,CAD,1.3;USD,GBP,0.71;USD,JPY,109;GBP,JPY,155
USD
JPY
Expected Output:
110.05