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] = [startCurrencyi, targetCurrencyi]
denotes that you can convert from startCurrencyi
to targetCurrencyi
at a rate of rates1[i]
on day 1.pairs2[i] = [startCurrencyi, targetCurrencyi]
denotes that you can convert from startCurrencyi
to targetCurrencyi
at a rate of rates2[i]
on day 2.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.
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:
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 <= 3
initialCurrency
consists only of uppercase English letters.1 <= n == pairs1.length <= 10
1 <= m == pairs2.length <= 10
pairs1[i] == [startCurrencyi, targetCurrencyi]
pairs2[i] == [startCurrencyi, targetCurrencyi]
1 <= startCurrencyi.length, targetCurrencyi.length <= 3
startCurrencyi
and targetCurrencyi
consist only of uppercase English letters.rates1.length == n
rates2.length == m
1.0 <= rates1[i], rates2[i] <= 10.0
5 * 1010
.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force strategy is to explore every possible combination of actions we can take over two days. We will look at every possibility and choose the best outcome. This approach guarantees finding the maximum amount if we consider all valid conversions.
Here's how the algorithm would work step-by-step:
def maximize_amount_after_two_days_brute_force(item_counts, conversion_rates):
maximum_amount = 0
# Iterate through all possible conversions on the first day
for first_day_conversion_count in range(item_counts[0] + 1):
remaining_item_count_after_first_conversion = [
item_counts[0] - first_day_conversion_count,
item_counts[1] + first_day_conversion_count * conversion_rates[0]
]
# Iterate through all possible conversions on the second day
for second_day_conversion_count in range(remaining_item_count_after_first_conversion[0] + 1):
# Calculates result after a second conversion
final_item_count_after_second_conversion = [
remaining_item_count_after_first_conversion[0] - second_day_conversion_count,
remaining_item_count_after_first_conversion[1] + second_day_conversion_count * conversion_rates[0]
]
final_amount = final_item_count_after_second_conversion[1]
# Keep track of the best final amount
maximum_amount = max(maximum_amount, final_amount)
return maximum_amount
The best way to tackle this problem is to figure out which conversion yields the most money each day and do it. The key is to repeatedly identify and exploit the most profitable conversion available at each step, focusing on immediate gains.
Here's how the algorithm would work step-by-step:
def maximize_amount(starting_amount, conversion_rates_day_one, conversion_rates_day_two):
current_amount = starting_amount
# Determine best conversion for day one
best_conversion_day_one_amount = 0
for rate in conversion_rates_day_one:
potential_amount = current_amount * rate
if potential_amount > best_conversion_day_one_amount:
best_conversion_day_one_amount = potential_amount
# Update the amount after day one's conversion
current_amount = best_conversion_day_one_amount
# Determine best conversion for day two
best_conversion_day_two_amount = 0
for rate in conversion_rates_day_two:
potential_amount = current_amount * rate
if potential_amount > best_conversion_day_two_amount:
best_conversion_day_two_amount = potential_amount
# Update the amount after day two's conversion
current_amount = best_conversion_day_two_amount
return current_amount
Case | How to Handle |
---|---|
Empty list of conversion rates | Return 0 immediately, as no conversions can be made. |
List of conversion rates contains only one element | Return the single element's value since no conversion possible. |
Very large conversion rates causing integer overflow during multiplication | Use a data type with larger capacity like long or double to store intermediate results, or use modular arithmetic if appropriate for the problem. |
Conversion rates are zero | Handle zero conversion rates correctly, which would result in a zero amount after conversions. |
Negative conversion rates (if permitted by the problem description) | Account for negative values, potentially leading to minimizing the amount instead of maximizing. |
All conversion rates are identical | The solution should still work correctly and perform the conversions as expected. |
Input list is extremely large (e.g., millions of entries), potentially leading to performance issues. | Ensure algorithm has optimal time complexity, potentially using dynamic programming if applicable, and consider memory usage implications. |
No profitable conversions are possible (all rates are less than or equal to 1) | The solution should still return the initial amount without performing any conversion. |