Minimum Cost For Tickets (LeetCode 983): Solution and Explanation
You know the days of the year you will travel. Passes for 1, 7 and 30 consecutive days cost different amounts. Find the cheapest way to cover every trip.
Intuition
Greedy rules like "buy a weekly pass whenever there are three trips in a week" break easily, because a pass bought now changes what is cheapest later. A table over the calendar avoids guessing.
Look at a trip on day d. Some pass covers it. If you imagine that pass ending exactly on day d, it reaches back 1, 7 or 30 days, and all trips before that window form a smaller problem you have already solved.
Days without a trip cost nothing extra, so their value is the same as the day before. That makes the whole thing a single pass over at most 365 days.
Approach: DP over calendar days
- Let
dp[d]be the cheapest cost that covers every trip on days 1..d, withdp[0] = 0. - For d from 1 to the last travel day: if d is not a travel day,
dp[d] = dp[d - 1]. - Otherwise try all three passes ending on day d:
dp[d - 1] + costs[0],dp[max(0, d - 7)] + costs[1]anddp[max(0, d - 30)] + costs[2]. - Store the smallest of the three in
dp[d]. - Return
dp[last].
Step-by-step example
days = [1,4,6,7,8,20], costs = [2,7,15]
- Day 1: options are 0 + 2, 0 + 7, 0 + 15, so dp[1] = 2. Days 2 and 3 have no trip and stay at 2.
- Day 4: 2 + 2 = 4 beats 0 + 7 and 0 + 15, so dp[4] = 4. Day 6: dp[6] = 6.
- Day 7: 1-day gives 8 but a 7-day pass covering days 1 to 7 gives 0 + 7 = 7, so dp[7] = 7.
- Day 8: 1-day gives 9, 7-day (days 2 to 8) gives dp[1] + 7 = 9, 30-day gives 15, so dp[8] = 9.
- Day 20: 1-day gives 9 + 2 = 11, 7-day gives dp[13] + 7 = 16, 30-day gives 15. dp[20] = 11, the answer.
Code: Python, Java, C++
1class Solution:
2 def mincostTickets(self, days: List[int], costs: List[int]) -> int:
3 last = days[-1]
4 travel = set(days)
5 dp = [0] * (last + 1)
6 for d in range(1, last + 1):
7 if d not in travel:
8 dp[d] = dp[d - 1]
9 continue
10 dp[d] = min(dp[d - 1] + costs[0],
11 dp[max(0, d - 7)] + costs[1],
12 dp[max(0, d - 30)] + costs[2])
13 return dp[last]1class Solution {
2 public int mincostTickets(int[] days, int[] costs) {
3 int last = days[days.length - 1];
4 boolean[] travel = new boolean[last + 1];
5 for (int d : days) {
6 travel[d] = true;
7 }
8 int[] dp = new int[last + 1];
9 for (int d = 1; d <= last; d++) {
10 if (!travel[d]) {
11 dp[d] = dp[d - 1];
12 continue;
13 }
14 dp[d] = Math.min(dp[d - 1] + costs[0],
15 Math.min(dp[Math.max(0, d - 7)] + costs[1],
16 dp[Math.max(0, d - 30)] + costs[2]));
17 }
18 return dp[last];
19 }
20}1class Solution {
2public:
3 int mincostTickets(vector<int>& days, vector<int>& costs) {
4 int last = days.back();
5 vector<bool> travel(last + 1, false);
6 for (int d : days) {
7 travel[d] = true;
8 }
9 vector<int> dp(last + 1, 0);
10 for (int d = 1; d <= last; d++) {
11 if (!travel[d]) {
12 dp[d] = dp[d - 1];
13 continue;
14 }
15 dp[d] = min(dp[d - 1] + costs[0],
16 min(dp[max(0, d - 7)] + costs[1],
17 dp[max(0, d - 30)] + costs[2]));
18 }
19 return dp[last];
20 }
21};Time and space complexity
O(W)
W is the last travel day, at most 365, and each day does constant work.
O(W)
One dp entry per calendar day up to the last trip. Keeping only the last 30 values would make it O(30).
Other approaches
DP over trip indices (time O(n), space O(n))
Let dp[i] be the cheapest cost for trips i..n-1. For each pass, jump to the first trip outside its window with a pointer or binary search. Useful when the calendar is huge but trips are few.
Plain recursion (time O(3^n), space O(n))
At each uncovered trip, try all three passes and recurse. Without memoisation the same suffixes are solved again and again, giving exponential time.
Common mistakes
- Indexing below zero. dp[d - 7] and dp[d - 30] must be clamped to dp[0] for early days.
- Assuming the longer pass is always better per day. Costs are arbitrary, so a 7-day pass can be cheaper than a 1-day pass, or a 30-day pass can be the most expensive.
- Charging for days without trips. Non-travel days should just copy the previous value.
- Running the loop to 365 instead of the last travel day. It still works but does extra work and needs a 366-slot array.
Frequently asked questions
Why can I assume the pass ends on the trip day?
Any pass covering day d can be shifted so it ends on d without uncovering an earlier trip it was needed for, provided you also count trips before the window separately, which dp does. So restricting to passes that end on d loses nothing.
What if the 7-day pass is cheaper than the 1-day pass?
The recurrence handles it automatically, because it always compares all three options. A single trip can then be covered by a 7-day pass.
Can I use less memory?
Yes. dp[d] only looks back at most 30 days, so a circular buffer of 30 values is enough, though with W at most 365 the full array is already tiny.