Minimum Round Trip Cost
EasyArrays · Meta
You are booking a round trip. departing[i] is the fare for leaving on day i, and returning[j] is the fare for coming back on day j. Both lists cover the same days, so they have the same length.
Choose a departure day i and a return day j with j > i (you must come back on a strictly later day). Return the minimum possible total fare departing[i] + returning[j].
If no valid pair of days exists (there is only one day), return -1.
Example 1
- Input:
- departing = [6, 3, 7, 1], returning = [9, 8, 2, 5]
- Output:
- 5
- Explanation
- Leaving on day 1 for 3 and returning on day 2 for 2 costs 5. The cheapest departure, 1 on day 3, is the last day, so there is no later day to return on.
Example 2
- Input:
- departing = [4, 9], returning = [10, 6]
- Output:
- 10
- Explanation
- The only valid choice is leave on day 0 and return on day 1, costing
4 + 6 = 10.
Example 3
- Input:
- departing = [5], returning = [3]
- Output:
- -1
- Explanation
- With a single day there is no later day to return on.
Constraints
1 <= departing.length == returning.length <= 10^51 <= departing[i], returning[i] <= 10^9