Problem · Array
Minimum Cost for Tickets
Learn this problemProblem statement
You travel on every day listed in the strictly increasing array days. A travel pass is valid for 1, 7, or 30 consecutive calendar days, including its purchase day. Their costs are costs[0], costs[1], and costs[2].
Return the minimum total cost needed to cover every travel day.
Function
minTicketCost(days: int[], costs: int[]) → intExamples
Example 1
days = [1,4,6,7,8,20]costs = [2,7,15]return = 11Buy a 1-day pass on day 1, a 7-day pass on day 4, and a 1-day pass on day 20.
Example 2
days = [1,2,3,4,5,6,7,8,9,10,30,31]costs = [2,7,15]return = 17A 30-day pass starting on day 1 covers days 1 through 30, and a 1-day pass covers day 31.
Constraints
1 <= days.length <= 365.1 <= days[i] <= 365anddaysis strictly increasing.costs.length = 3.1 <= costs[i] <= 1000.