|
| 1 | +3387\. Maximize Amount After Two Days of Conversions |
| 2 | + |
| 3 | +Medium |
| 4 | + |
| 5 | +You are given a string `initialCurrency`, and you start with `1.0` of `initialCurrency`. |
| 6 | + |
| 7 | +You are also given four arrays with currency pairs (strings) and rates (real numbers): |
| 8 | + |
| 9 | +* <code>pairs1[i] = [startCurrency<sub>i</sub>, targetCurrency<sub>i</sub>]</code> denotes that you can convert from <code>startCurrency<sub>i</sub></code> to <code>targetCurrency<sub>i</sub></code> at a rate of `rates1[i]` on **day 1**. |
| 10 | +* <code>pairs2[i] = [startCurrency<sub>i</sub>, targetCurrency<sub>i</sub>]</code> denotes that you can convert from <code>startCurrency<sub>i</sub></code> to <code>targetCurrency<sub>i</sub></code> at a rate of `rates2[i]` on **day 2**. |
| 11 | +* Also, each `targetCurrency` can be converted back to its corresponding `startCurrency` at a rate of `1 / rate`. |
| 12 | + |
| 13 | +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. |
| 14 | + |
| 15 | +Return the **maximum** amount of `initialCurrency` you can have after performing any number of conversions on both days **in order**. |
| 16 | + |
| 17 | +**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. |
| 18 | + |
| 19 | +**Example 1:** |
| 20 | + |
| 21 | +**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] |
| 22 | + |
| 23 | +**Output:** 720.00000 |
| 24 | + |
| 25 | +**Explanation:** |
| 26 | + |
| 27 | +To get the maximum amount of **EUR**, starting with 1.0 **EUR**: |
| 28 | + |
| 29 | +* On Day 1: |
| 30 | + * Convert **EUR** to **USD** to get 2.0 **USD**. |
| 31 | + * Convert **USD** to **JPY** to get 6.0 **JPY**. |
| 32 | +* On Day 2: |
| 33 | + * Convert **JPY** to **USD** to get 24.0 **USD**. |
| 34 | + * Convert **USD** to **CHF** to get 120.0 **CHF**. |
| 35 | + * Finally, convert **CHF** to **EUR** to get 720.0 **EUR**. |
| 36 | + |
| 37 | +**Example 2:** |
| 38 | + |
| 39 | +**Input:** initialCurrency = "NGN", pairs1 = [["NGN","EUR"]], rates1 = [9.0], pairs2 = [["NGN","EUR"]], rates2 = [6.0] |
| 40 | + |
| 41 | +**Output:** 1.50000 |
| 42 | + |
| 43 | +**Explanation:** |
| 44 | + |
| 45 | +Converting **NGN** to **EUR** on day 1 and **EUR** to **NGN** using the inverse rate on day 2 gives the maximum amount. |
| 46 | + |
| 47 | +**Example 3:** |
| 48 | + |
| 49 | +**Input:** initialCurrency = "USD", pairs1 = [["USD","EUR"]], rates1 = [1.0], pairs2 = [["EUR","JPY"]], rates2 = [10.0] |
| 50 | + |
| 51 | +**Output:** 1.00000 |
| 52 | + |
| 53 | +**Explanation:** |
| 54 | + |
| 55 | +In this example, there is no need to make any conversions on either day. |
| 56 | + |
| 57 | +**Constraints:** |
| 58 | + |
| 59 | +* `1 <= initialCurrency.length <= 3` |
| 60 | +* `initialCurrency` consists only of uppercase English letters. |
| 61 | +* `1 <= n == pairs1.length <= 10` |
| 62 | +* `1 <= m == pairs2.length <= 10` |
| 63 | +* <code>pairs1[i] == [startCurrency<sub>i</sub>, targetCurrency<sub>i</sub>]</code> |
| 64 | +* <code>pairs2[i] == [startCurrency<sub>i</sub>, targetCurrency<sub>i</sub>]</code> |
| 65 | +* <code>1 <= startCurrency<sub>i</sub>.length, targetCurrency<sub>i</sub>.length <= 3</code> |
| 66 | +* <code>startCurrency<sub>i</sub></code> and <code>targetCurrency<sub>i</sub></code> consist only of uppercase English letters. |
| 67 | +* `rates1.length == n` |
| 68 | +* `rates2.length == m` |
| 69 | +* `1.0 <= rates1[i], rates2[i] <= 10.0` |
| 70 | +* The input is generated such that there are no contradictions or cycles in the conversion graphs for either day. |
0 commit comments