Minimum Cost For Tickets

Medium

Topics
ArrayDynamic Programming

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

travel days120[1,1][4,4][6,6][7,7][8,8][20,20]
passes bought120[1,1][4,10][20,20]
a day you travela pass, drawn over the days it covers
Travelling on 6 days costs 11 at best, bought as 3 passes: 1-day for 2, 7-day for 7, 1-day for 2. A pass covers a run of consecutive calendar days from the day it is bought, whether or not you travel on them, so what matters is how your travel days cluster rather than how many there are. Prices are arbitrary, so a longer pass can be the right buy even for a single day.
The three prices are the 1-day, 7-day and 30-day pass, in that order.
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

travel days131[1,1][2,2][3,3][4,4][5,5][6,6][7,7][8,8][9,9][10,10][30,30][31,31]
passes bought131[1,1][2,2][3,3][4,10][30,30][31,31]
a day you travela pass, drawn over the days it covers
Travelling on 12 days costs 17 at best, bought as 6 passes: 1-day for 2, 1-day for 2, 1-day for 2, 7-day for 7, 1-day for 2, 1-day for 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

travel days23[2,2]
passes bought28[2,8]
a day you travela pass, drawn over the days it covers
Travelling on 1 day costs 1 at best, bought as 1 pass: 7-day for 1.
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

  1. Index the travel days. Define best(i) as the minimum cost to cover travel day i onwards, with best(n) = 0.
  2. For each of the three durations 1, 7 and 30: the pass bought on day a[i] covers up to a[i] + duration - 1.
  3. Find the first index j > i whose day exceeds that, which is where the next pass must start.
  4. The cost of that choice is price + best(j), and best(i) is the smallest over the three choices.
  5. Compute from the last travel day backwards so each best(j) is known when needed. Advancing j with 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 11

Complexity

  • 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 d covers through d + 6, not d + 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. b is 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]
SensAI
Beta
Tap the mic to stop voice mode