3387. Maximize Amount After Two Days of Conversions Medium
1/**
2 * [3387] Maximize Amount After Two Days of Conversions
3 *
4 * You are given a string initialCurrency, and you start with 1.0 of initialCurrency.
5 * You are also given four arrays with currency pairs (strings) and rates (real numbers):
6 *
7 * pairs1[i] = [startCurrencyi, targetCurrencyi] denotes that you can convert from startCurrencyi to targetCurrencyi at a rate of rates1[i] on day 1.
8 * pairs2[i] = [startCurrencyi, targetCurrencyi] denotes that you can convert from startCurrencyi to targetCurrencyi at a rate of rates2[i] on day 2.
9 * Also, each targetCurrency can be converted back to its corresponding startCurrency at a rate of 1 / rate.
10 *
11 * 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.
12 * Return the maximum amount of initialCurrency you can have after performing any number of conversions on both days in order.
13 * 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.
14 *
15 * <strong class="example">Example 1:
16 * <div class="example-block">
17 * Input: <span class="example-io">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]</span>
18 * Output: <span class="example-io">720.00000</span>
19 * Explanation:
20 * To get the maximum amount of EUR, starting with 1.0 EUR:
21 *
22 * On Day 1:
23 *
24 * Convert EUR to USD to get 2.0 USD.
25 * Convert USD to JPY to get 6.0 JPY.
26 *
27 *
28 * On Day 2:
29 *
30 * Convert JPY to USD to get 24.0 USD.
31 * Convert USD to CHF to get 120.0 CHF.
32 * Finally, convert CHF to EUR to get 720.0 EUR.
33 *
34 *
35 * </div>
36 * <strong class="example">Example 2:
37 * <div class="example-block">
38 * Input: <span class="example-io">initialCurrency = "NGN", pairs1 = </span>[["NGN","EUR"]]<span class="example-io">, rates1 = </span>[9.0]<span class="example-io">, pairs2 = </span>[["NGN","EUR"]]<span class="example-io">, rates2 = </span>[6.0]
39 * Output: 1.50000
40 * Explanation:
41 * Converting NGN to EUR on day 1 and EUR to NGN using the inverse rate on day 2 gives the maximum amount.
42 * </div>
43 * <strong class="example">Example 3:
44 * <div class="example-block">
45 * Input: <span class="example-io">initialCurrency = "USD", pairs1 = [["USD","EUR"]], rates1 = [1.0], pairs2 = [["EUR","JPY"]], rates2 = [10.0]</span>
46 * Output: <span class="example-io">1.00000</span>
47 * Explanation:
48 * In this example, there is no need to make any conversions on either day.
49 * </div>
50 *
51 * Constraints:
52 *
53 * 1 <= initialCurrency.length <= 3
54 * initialCurrency consists only of uppercase English letters.
55 * 1 <= n == pairs1.length <= 10
56 * 1 <= m == pairs2.length <= 10
57 * pairs1[i] == [startCurrencyi, targetCurrencyi]<!-- notionvc: c31b5bb8-4df6-4987-9bcd-6dff8a5f7cd4 -->
58 * pairs2[i] == [startCurrencyi, targetCurrencyi]<!--{C}%3C!%2D%2D%20notionvc%3A%20c31b5bb8-4df6-4987-9bcd-6dff8a5f7cd4%20%2D%2D%3E-->
59 * 1 <= startCurrencyi.length, targetCurrencyi.length <= 3
60 * startCurrencyi and targetCurrencyi consist only of uppercase English letters.
61 * rates1.length == n
62 * rates2.length == m
63 * 1.0 <= rates1[i], rates2[i] <= 10.0
64 * The input is generated such that there are no contradictions or cycles in the conversion graphs for either day.
65 * The input is generated such that the output is at most 5 * 10^10.
66 *
67 */
68pub struct Solution {}
69
70// problem: https://leetcode.com/problems/maximize-amount-after-two-days-of-conversions/
71// discuss: https://leetcode.com/problems/maximize-amount-after-two-days-of-conversions/discuss/?currentPage=1&orderBy=most_votes&query=
72
73// submission codes start here
74
75impl Solution {
76 pub fn max_amount(initial_currency: String, pairs1: Vec<Vec<String>>, rates1: Vec<f64>, pairs2: Vec<Vec<String>>, rates2: Vec<f64>) -> f64 {
77 0f64
78 }
79}
80
81// submission codes end
82
83#[cfg(test)]
84mod tests {
85 use super::*;
86
87 #[test]
88 fn test_3387() {
89 }
90}
91
Back
© 2025 bowen.ge All Rights Reserved.