Facebook Pixel

Minimum Cost For Tickets

You have planned some train travel a year in advance. The days of the year on which you will travel are given as an integer array days, sorted in ascending order, where each day is a number between 1 and 365.

Train tickets are sold three ways: a 1-day pass costs costs[0] dollars, a 7-day pass costs costs[1] dollars, and a 30-day pass costs costs[2] dollars. A pass bought on a given day covers that many consecutive calendar days starting from it, whether or not you travel on all of them.

Return the minimum number of dollars needed to cover every day in days.

Input & Output
Input
days — the sorted list of days you will travel
costs — the prices of the 1-day, 7-day, and 30-day passes
Output
the minimum total dollars needed to cover every travel day
Example
Input
days = [1, 4, 6, 7, 8, 20], costs = [2, 7, 15]
Output
11
Explanation

Buy a 1-day pass on day 1 for 2, a 7-day pass on day 4 covering days 4 through 10 for 7, and a 1-day pass on day 20 for 2. The total is 11.

Example
Input
days = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31], costs = [2, 7, 15]
Output
17
Explanation

Buy a 30-day pass on day 1 for 15, covering days 1 through 30, then a 1-day pass on day 31 for 2. Buying 1-day passes for all twelve days would cost 24, and the ten early days alone would cost 20.

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

Try it yourself

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro