Maximize Amount After Two Days of Conversions - Problem
You are given a string initialCurrency, and you start with 1.0 of initialCurrency.
You are also given four arrays with currency pairs (strings) and rates (real numbers):
pairs1[i] = [startCurrency_i, targetCurrency_i]denotes that you can convert fromstartCurrency_itotargetCurrency_iat a rate ofrates1[i]on day 1.pairs2[i] = [startCurrency_i, targetCurrency_i]denotes that you can convert fromstartCurrency_itotargetCurrency_iat a rate ofrates2[i]on day 2.
Also, each targetCurrency can be converted back to its corresponding startCurrency at a rate of 1 / rate.
You can perform any number of conversions, including zero, using rates1 on day 1, followed by any number of additional conversions, including zero, using rates2 on day 2.
Return the maximum amount of initialCurrency you can have after performing any number of conversions on both days in order.
Input & Output
Example 1 — Basic Case
$
Input:
initialCurrency = "USD", pairs1 = [["EUR","USD"],["USD","JPY"]], rates1 = [2.0,3.0], pairs2 = [["JPY","USD"],["USD","EUR"],["EUR","JPY"]], rates2 = [4.0,5.0,6.0]
›
Output:
20.0
💡 Note:
Day 1: USD → JPY (3.0), Day 2: JPY → EUR (6.0) → USD (5.0). Total: 1.0 × 3.0 × 6.0 × 5.0 = 90.0, but optimal path gives 20.0
Example 2 — No Conversion
$
Input:
initialCurrency = "USD", pairs1 = [["EUR","GBP"]], rates1 = [1.5], pairs2 = [["GBP","JPY"]], rates2 = [2.0]
›
Output:
1.0
💡 Note:
USD cannot be converted using available pairs, so return initial amount 1.0
Example 3 — Round Trip
$
Input:
initialCurrency = "USD", pairs1 = [["USD","EUR"]], rates1 = [0.85], pairs2 = [["EUR","USD"]], rates2 = [1.20]
›
Output:
1.02
💡 Note:
Day 1: USD → EUR (0.85), Day 2: EUR → USD (1.20). Total: 1.0 × 0.85 × 1.20 = 1.02
Constraints
- 1 ≤ initialCurrency.length ≤ 3
- 1 ≤ pairs1.length, pairs2.length ≤ 10
- 1 ≤ rates1[i], rates2[i] ≤ 10
- All currencies are 3-letter codes
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code