Find Lowest Cost Ticket Pair Across Consecutive Days
Arrays & HashingAuto-graded · 18 test casesLast updated
Find the minimum combined cost of an outbound and return ticket where return happens strictly after departure.
You are given two integer lists of equal length $n$, representing flight prices over a sequence of days indexed from $0$ to $n-1$:
departure[i]: cost to leave on day $i$.arrival[j]: cost to return on day $j$.
A valid itinerary requires leaving on some day $i$ and returning on some day $j$ strictly later ($j > i$). The total itinerary cost for this selection is departure[i] + arrival[j].
Find the minimum total cost among all valid combinations $(i, j)$ with $j > i$. If no valid itinerary can be formed (that is, when $n < 2$), return -1.
Constraints
- $1 \le n \le 100{,}000$
len(departure) == len(arrival) == n- $0 \le \text{departure}[i], \text{arrival}[j] \le 10^9$
Examples
Example 1
Input: minimum_round_trip_cost([4, 2, 5, 1], [6, 3, 2, 7])
Output: 4
Example 2
Input: minimum_round_trip_cost([5], [5])
Output: -1
Hints
Hint 1
A brute force search checks all pairs (i, j) with i < j in O(n^2) time. Can you find the optimal outbound day for a fixed return day j in O(1)?
Hint 2
For any given return day j, the optimal outbound day is the one with the smallest departure cost among days 0 through j-1.
Hint 3
Track the running minimum departure cost as you iterate j from 1 to n-1. Be careful to combine arrival[j] with the running minimum *before* incorporating departure[j] into the minimum.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Method Explanation
To solve this problem efficiently in $O(n)$ time, consider what happens when we fix a return day $j$. To minimize departure[i] + arrival[j] for a fixed $j$, we need to pair arrival[j] with the smallest departure cost available on any day $i < j$.
Instead of recomputing the minimum departure cost from scratch for each day, we can maintain a running variable best_departure that stores the minimum of departure[0] through departure[j - 1].
As we iterate $j$ from $1$ to $n - 1$:
- Calculate the candidate cost using the current
best_departure(which covers days $0 \dots j-1$) andarrival[j]. - Update
best_departurewithdeparture[j]so that day $j$'s departure cost becomes eligible for return days greater than $j$.
The sequence of operations ensures that $i < j$ strictly holds, avoiding same-day departures and returns. If $n < 2$, no valid pair exists, so we return -1.
Complexity Analysis
- Time Complexity: $O(n)$ because we perform a single linear scan over the lists.
- Space Complexity: $O(1)$ additional space beyond the input arrays.
Python Source Code
from typing import List
def minimum_round_trip_cost(departure: List[int], arrival: List[int]) -> int:
n = len(departure)
if n < 2:
return -1
best_departure = departure[0]
answer = float("inf")
for j in range(1, n):
# Evaluate valid trip ending on day j
answer = min(answer, best_departure + arrival[j])
# Update running minimum departure cost for future return days
best_departure = min(best_departure, departure[j])
return int(answer)