Minimum Cost For Tickets
Medium
Topics
a lists the days of the year you will travel, in strictly increasing order. b holds three ticket prices: a 1-day pass, a 7-day pass and a 30-day pass, in that order.
A pass bought on day d covers day d and the following duration - 1 days, so a 7-day pass bought on day 2 covers days 2 through 8. Return the minimum you must spend to cover every travel day.
LeetCode names the arguments days and costs. The harness here supplies two integer arrays called a and b: a is the travel days and b is the three prices.
Example 1
Input: a = [1,4,6,7,8,20], b = [2,7,15] Output: 11 Explanation: A 1-day pass on day 1 for 2, a 7-day pass on day 4 covering days 4 to 10 for 7, and a 1-day pass on day 20 for 2.
Example 2
Input: a = [1,2,3,4,5,6,7,8,9,10,30,31], b = [2,7,15] Output: 17 Explanation: A 30-day pass on day 1 covers days 1 to 30 for 15, then a 1-day pass on day 31 for 2.
Example 3
Input: a = [2], b = [5,1,3] Output: 1 Explanation: The 7-day pass is cheaper than the 1-day pass here, so buy it even for a single day.
Constraints
- 1 <= a.length <= 365
- 1 <= a[i] <= 365
- a is strictly increasing
- b.length == 3
- 1 <= b[i] <= 1000
Editorial — intuition, approach, complexity and pitfalls
Intuition
The trap is assuming a longer pass is worth buying only when you travel enough days to "use it up". Example 3 kills that: the 7-day pass costs less than the 1-day pass, so it is the right buy for a single day of travel. Prices are arbitrary and no such reasoning is safe.
What is safe is to work backwards from each travel day and ask: what is the cheapest way to cover this day and everything after it? Whatever pass covers this day, it must start today — there is no reason to start a pass on a day you are not travelling, since starting it later covers at least as much.
So from a travel day there are exactly three choices, and each one jumps you forward to the first travel day the pass does not cover. Take the cheapest of the three.
Walking the travel days rather than all 365 calendar days keeps the work proportional to the trips. Walking the calendar is also fine at this size and is arguably simpler to write, since a non-travel day just inherits the cost of the day before.
Approach
- Index the travel days. Define
best(i)as the minimum cost to cover travel dayionwards, withbest(n) = 0. - For each of the three durations 1, 7 and 30: the pass bought on day
a[i]covers up toa[i] + duration - 1. - Find the first index
j > iwhose day exceeds that, which is where the next pass must start. - The cost of that choice is
price + best(j), andbest(i)is the smallest over the three choices. - Compute from the last travel day backwards so each
best(j)is known when needed. Advancingjwith a scan is enough at this size; a halving search over the sorted days is the faster form.
Walkthrough
a = [1,4,6,7,8,20], b = [2,7,15]
best(i) = cheapest way to cover a[i..end]
best(6) = 0 (nothing left)
i=5, day 20
1-day : 2 + best(6)=0 = 2
7-day : 7 + 0 = 7
30-day: 15 + 0 = 15
best(5) = 2
i=4, day 8 (covers to 8, 14, 37)
1-day : 2 + best(5)=2 = 4
7-day : 7 + best(5)=2 = 9
30-day: 15 + best(6)=0 = 15
best(4) = 4
i=3, day 7 (covers to 7, 13, 36)
1-day : 2 + best(4)=4 = 6
7-day : 7 + best(5)=2 = 9
30-day: 15 + 0 = 15
best(3) = 6
i=2, day 6 (covers to 6, 12, 35)
1-day : 2 + best(3)=6 = 8
7-day : 7 + best(5)=2 = 9
30-day: 15 = 15
best(2) = 8
i=1, day 4 (covers to 4, 10, 33)
1-day : 2 + best(2)=8 = 10
7-day : 7 + best(5)=2 = 9 <-- covers days 4..10, i.e. 4,6,7,8
30-day: 15 = 15
best(1) = 9
i=0, day 1 (covers to 1, 7, 30)
1-day : 2 + best(1)=9 = 11
7-day : 7 + best(4)=4 = 11
30-day: 15 + best(5)=2 = 17
best(0) = 11
answer 11Complexity
- Time — O(n * 30): each travel day considers three passes, and the forward scan skips at most 30 days.
- Space — O(n): one entry per travel day.
Common Pitfalls
- Assuming a longer pass is only worth buying when enough days fall inside it. Prices are arbitrary, as example 3 shows.
- Off-by-one on coverage. A 7-day pass bought on day
dcovers throughd + 6, notd + 7. - Starting a pass on a non-travel day. It is never better, so the choices can be restricted to travel days.
- Reading the prices in the wrong order.
bis 1-day, 7-day, 30-day. - Indexing the cost array by calendar day and forgetting that days run to 365, or by travel index and forgetting the terminating zero.
Full Solution — all languages
class Solution:
def mincostTickets(self, a, b):
n = len(a)
durations = (1, 7, 30)
# best[i] = cheapest cover for travel days a[i:]
best = [0] * (n + 1)
for i in range(n - 1, -1, -1):
best[i] = float("inf")
for price, duration in zip(b, durations):
# A pass bought today covers through this last day.
last = a[i] + duration - 1
# First travel day the pass does not reach.
j = i
while j < n and a[j] <= last:
j += 1
best[i] = min(best[i], price + best[j])
return best[0]import java.util.*;
class Solution {
public int mincostTickets(int[] a, int[] b) {
int n = a.length;
int[] durations = {1, 7, 30};
int[] best = new int[n + 1];
for (int i = n - 1; i >= 0; i--) {
best[i] = Integer.MAX_VALUE;
for (int p = 0; p < 3; p++) {
int last = a[i] + durations[p] - 1;
int j = i;
while (j < n && a[j] <= last) j++;
best[i] = Math.min(best[i], b[p] + best[j]);
}
}
return best[0];
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int mincostTickets(vector<int>& a, vector<int>& b) {
int n = (int)a.size();
int durations[3] = {1, 7, 30};
vector<int> best(n + 1, 0);
for (int i = n - 1; i >= 0; i--) {
best[i] = INT_MAX;
for (int p = 0; p < 3; p++) {
int last = a[i] + durations[p] - 1;
int j = i;
while (j < n && a[j] <= last) j++;
best[i] = min(best[i], b[p] + best[j]);
}
}
return best[0];
}
};/**
* @param {number[]} a
* @param {number[]} b
* @return {number}
*/ var mincostTickets = function(a, b) {
const n = a.length;
const durations = [1, 7, 30];
const best = new Array(n + 1).fill(0);
for (let i = n - 1; i >= 0; i--) {
best[i] = Infinity;
for (let p = 0; p < 3; p++) {
const last = a[i] + durations[p] - 1;
let j = i;
while (j < n && a[j] <= last) j++;
best[i] = Math.min(best[i], b[p] + best[j]);
}
}
return best[0];
};