Minimum Cost For Tickets

MediumDynamic Programming1d dp

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You will travel on the days listed in days (strictly increasing integers between 1 and 365). Travel passes come in three kinds, with prices given by costs: - costs[0] buys a 1-day pass, - costs[1] buys a 7-day pass, - costs[2] buys a 30-day pass. A pass bought for day d covers days d through d + length - 1 inclusive. Return the minimum total amount you must spend so that every travel day is covered. Prices are not guaranteed to be monotonic (a longer pass may be cheaper).

Examples

Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
1-day pass on day 1 (2), 7-day pass on day 4 covering 4..10 (7), 1-day pass on day 20 (2).
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
30-day pass on day 1 covers 1..30 (15), 1-day pass on day 31 (2).

Constraints

  • 1 <= days.length <= 365
  • 1 <= days[i] <= 365
  • days is strictly increasing
  • costs.length == 3
  • 1 <= costs[i] <= 1000

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More 1d dp problems