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 from startCurrency_i to targetCurrency_i at a rate of rates1[i] on day 1.
  • pairs2[i] = [startCurrency_i, targetCurrency_i] denotes that you can convert from startCurrency_i to targetCurrency_i at a rate of rates2[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
INPUTCurrency GraphUSDEURJPY2.03.0Day 1 Rates:EUR→USD: 2.0USD→JPY: 3.0Day 2 Rates:JPY→USD: 4.0USD→EUR: 5.0EUR→JPY: 6.0ALGORITHMFloyd-Warshall Steps1Build Rate MatricesCreate adjacency matrices for both days2Apply Floyd-WarshallFind max rates via intermediate currencies3Combine Daysday1[initial][k] × day2[k][initial]4Find MaximumReturn best conversion amountTime: O(V³)Space: O(V²)RESULTMaximum Amount20.0USD after 2 daysStarting from 1.0 USDOptimal Path:Day 1: USD → JPY (3.0)Amount: 3.0 JPYDay 2: JPY → EUR → USD3.0 × 6.0 × 5.0 = 90.0But optimal is 20.0Key Insight:Model currency conversion as graph shortest path problem.Floyd-Warshall finds maximum rates between all currency pairs efficiently.TutorialsPoint - Maximize Amount After Two Days of Conversions | Floyd-Warshall Algorithm
Asked in
Google 15 Amazon 12 Microsoft 8 Facebook 6
23.5K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen