Minimum Round-Trip Cost with Ordered Dates
Arrays & HashingAuto-graded · 23 test casesLast updated
Tests prefix-minimum optimization to select two array indices under ordering constraints while minimizing their combined cost.
You are planning a round-trip journey. You have two arrays of equal length representing costs on different days:
depart: an array wheredepart[i]is the cost to leave on dayireturn_costs: an array wherereturn_costs[j]is the cost to come back on dayj
You must choose a departure day i and a return day j such that you return strictly after you depart (i < j). The total cost of your trip is depart[i] + return_costs[j].
Find the minimum possible total cost and return the result as a list [total_cost, i, j] where i and j are the chosen days.
Tie-breaking: If multiple pairs (i, j) achieve the same minimum cost, return the lexicographically smallest pair. That is, prefer the smallest i first, and among those, the smallest j.
Edge case: If fewer than two days are available (making it impossible to satisfy i < j), return None.
Constraints
0 ≤ len(depart) ≤ 10^5len(depart) == len(return_costs)-10^6 ≤ depart[i], return_costs[j] ≤ 10^6- All costs are integers
Examples
Example 1
Input: min_round_trip_cost([5, 1, 4, 8], [7, 6, 2, 3])
Output: [3, 1, 2]
Example 2
Input: min_round_trip_cost([3, 1, 1, 5], [4, 2, 2, 1])
Output: [2, 1, 3]
Example 3
Input: min_round_trip_cost([-5, 2], [3, -4])
Output: [-9, 0, 1]
Hints
Hint 1
For each possible return day j, what is the best departure day you can pick from all earlier days?
Hint 2
Track the minimum departure cost seen so far as you scan through the days—this is a prefix minimum pattern.
Hint 3
Make sure to update your prefix minimum only after evaluating the current return day, so that you never pair day j with itself.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The brute-force approach would check every pair (i, j) with i < j, giving O(n²) time. We can do better by recognizing that for any return day j, the optimal departure day is simply the day with the minimum cost among all days before j.
This is a prefix minimum pattern. As we iterate through potential return days from left to right, we maintain:
- The minimum departure cost seen so far
- The earliest day that achieved that minimum (for tie-breaking)
For each return day j starting from day 1:
- Compute the total cost using the current best departure and return day
j - Update our answer if this total is better (or tied with a lexicographically smaller pair)
- After evaluating day j as a return day, check if day
jitself offers a cheaper departure for future return days
The ordering of steps 2 and 3 is crucial: we must not pair day j with itself, so we only consider it as a departure candidate for later return days.
Why It Works
The algorithm evaluates every valid pair (i, j) with i < j exactly once in an implicit way. When we process return day j, we're pairing it with the best possible departure from days 0 through j-1. Since we scan all return days and track the global minimum, we're guaranteed to find the optimal solution.
The tie-breaking works because:
- We only update the cheapest departure when we find a strictly smaller cost, preserving the earliest day
- We compare candidate pairs lexicographically, ensuring the smallest
(i, j)wins among ties
Complexity
Time: O(n) — single pass through the arrays
Space: O(1) — only a constant number of variables
Solution
from typing import List, Optional
def min_round_trip_cost(depart: List[int], return_costs: List[int]) -> Optional[List[int]]:
n = len(depart)
if n < 2:
return None
cheapest_depart = depart[0]
cheapest_depart_day = 0
best_total = None
best_days = None
for j in range(1, n):
total = cheapest_depart + return_costs[j]
candidate = [cheapest_depart_day, j]
if best_total is None or total < best_total or (total == best_total and candidate < best_days):
best_total = total
best_days = candidate
if depart[j] < cheapest_depart:
cheapest_depart = depart[j]
cheapest_depart_day = j
return [best_total, best_days[0], best_days[1]]