Problem · Array

Minimum Cost for Tickets

Learn this problem
MediumRoku logoRokuFULLTIMEONSITE INTERVIEW

Problem 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[]) → int

Examples

Example 1

days = [1,4,6,7,8,20]costs = [2,7,15]return = 11

Buy 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 = 17

A 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] <= 365 and days is strictly increasing.
  • costs.length = 3.
  • 1 <= costs[i] <= 1000.

More Roku problems

drafts saved locally
public int minTicketCost(int[] days, int[] costs) {
    // Write your code here.
}
days[1,4,6,7,8,20]
costs[2,7,15]
expected11
checking account