Maximize Amount After Two Days of Conversions Problem
Description
LeetCode Problem 3387.
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
targetCurrencycan be converted back to its correspondingstartCurrencyat a rate of1 / 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.
Note: Conversion rates are valid, and there will be no contradictions in the rates for either day. The rates for the days are independent of each other.
Example 1:
Input: initialCurrency = "EUR", pairs1 = [["EUR","USD"],["USD","JPY"]], rates1 = [2.0,3.0], pairs2 = [["JPY","USD"],["USD","CHF"],["CHF","EUR"]], rates2 = [4.0,5.0,6.0]
Output: 720.00000
Explanation:
To get the maximum amount of EUR, starting with 1.0 EUR:
- On Day 1:
- Convert EUR to USD to get 2.0 USD.
- Convert USD to JPY to get 6.0 JPY.
- On Day 2:
- Convert JPY to USD to get 24.0 USD.
- Convert USD to CHF to get 120.0 CHF.
- Finally, convert CHF to EUR to get 720.0 EUR.
Example 2:
Input: initialCurrency = "NGN", pairs1 = [["NGN","EUR"]], rates1 = [9.0], pairs2 = [["NGN","EUR"]], rates2 = [6.0]
Output: 1.50000
Explanation:
Converting NGN to EUR on day 1 and EUR to NGN using the inverse rate on day 2 gives the maximum amount.
Example 3:
Input: initialCurrency = "USD", pairs1 = [["USD","EUR"]], rates1 = [1.0], pairs2 = [["EUR","JPY"]], rates2 = [10.0]
Output: 1.00000
Explanation:
In this example, there is no need to make any conversions on either day.
Constraints:
1 <= initialCurrency.length <= 3initialCurrencyconsists only of uppercase English letters.1 <= n == pairs1.length <= 101 <= m == pairs2.length <= 10pairs1[i] == [startCurrency_i, targetCurrency_i]pairs2[i] == [startCurrency_i, targetCurrency_i]1 <= startCurrency_i.length, targetCurrency_i.length <= 3startCurrency_iandtargetCurrency_iconsist only of uppercase English letters.rates1.length == nrates2.length == m1.0 <= rates1[i], rates2[i] <= 10.0- The input is generated such that there are no contradictions or cycles in the conversion graphs for either day.
- The input is generated such that the output is at most
5 * 10^10.
Solution
Approach: DFS on both days
Because each day's conversion graph has no cycles or contradictions, the amount of every currency reachable from initialCurrency is the same along any path. A DFS from the initial currency over the day-1 graph (with each edge usable in both directions, the reverse at rate 1 / rate) gives the amount of each currency we can hold at the end of day 1.
Do the same on day 2: it gives how many units of each currency one unit of the initial currency buys. To end in the initial currency, the amount in currency c after day 1 is converted back by dividing by that day-2 factor. Take the maximum over all currencies reachable on both days (the initial currency itself gives 1.0).
Complexity: Time O(n + m), space O(n + m), where n and m are the numbers of pairs on each day.
from collections import defaultdict
class Solution:
def maxAmount(
self,
initialCurrency: str,
pairs1: List[List[str]],
rates1: List[float],
pairs2: List[List[str]],
rates2: List[float],
) -> float:
d1 = self.build(pairs1, rates1, initialCurrency)
d2 = self.build(pairs2, rates2, initialCurrency)
return max(d1.get(a, 0) / r2 for a, r2 in d2.items())
def build(
self, pairs: List[List[str]], rates: List[float], init: str
) -> Dict[str, float]:
def dfs(a: str, v: float):
d[a] = v
for b, r in g[a]:
if b not in d:
dfs(b, v * r)
g = defaultdict(list)
for (a, b), r in zip(pairs, rates):
g[a].append((b, r))
g[b].append((a, 1 / r))
d = {}
dfs(init, 1)
return d