AlgoFrames

Minimum cost for tickets

Standard · Write code · about 15 min

You travel on the days listed in days (sorted, between 1 and 365). Passes come in three kinds: 1 day for costs[0], 7 days for costs[1] and 30 days for costs[2]. A pass bought on day d covers d and the days after it, up to its length.

Return the lowest total cost that covers every travel day.

Example 1

Input:

[[1,4,6,7,8,20],[2,7,15]]

Output:

11

1-day pass on day 1, 7-day pass on day 4 (covers 4..10), 1-day pass on day 20: 2 + 7 + 2.

Example 2

Input:

[[1,2,3,4,5,6,7,8,9,10,30,31],[2,7,15]]

Output:

17

A 30-day pass on day 1 and a 1-day pass on day 31: 15 + 2.

Constraints

  • 1 ≤ days.length ≤ 365
  • 1 ≤ days[i] ≤ 365, strictly increasing
  • costs.length = 3
  • 1 ≤ costs[i] ≤ 500

Learn the idea first: 1-D DP: house robber, decode ways, jump game II, a visual lesson.

More 1-D DP: house robber, decode ways, jump game II problems