You have planned some train traveling one year in advance. The days of the year in which you will travel are given as an integer array days. Each day is an integer from 1 to 365.
Train tickets are sold in three different ways:
costs[0] dollars,costs[1] dollars, andcosts[2] dollars.The passes allow that many days of consecutive travel.
2, then we can travel for 7 days: 2, 3, 4, 5, 6, 7, and 8.Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15] Output: 11 Explanation: For example, here is one way to buy passes that lets you travel your travel plan: On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1. On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9. On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20. In total, you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15] Output: 17 Explanation: For example, here is one way to buy passes that lets you travel your travel plan: On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30. On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31. In total, you spent $17 and covered all the days of your travel.
Constraints:
1 <= days.length <= 3651 <= days[i] <= 365days is in strictly increasing order.costs.length == 31 <= costs[i] <= 1000When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To find the absolute cheapest way to cover all your travel days, the brute force strategy involves looking at every single combination of pass purchases and calculating the total cost for each one. By examining every possible choice for every single day you plan to travel, you can eventually compare all the totals and pick the smallest one.
Here's how the algorithm would work step-by-step:
class Solution:
def mincostTickets(self, travel_days: list[int], pass_costs: list[int]) -> int:
travel_days_set = set(travel_days)
last_travel_day = travel_days[-1]
def calculate_minimum_cost(current_day):
# Base case stops recursion once we pass the final day of travel to prevent out of bounds
if current_day > last_travel_day:
return 0
if current_day not in travel_days_set:
return calculate_minimum_cost(current_day + 1)
# Evaluate all three pass options to find the absolute cheapest branch moving forward
cost_with_one_day = pass_costs[0] + calculate_minimum_cost(current_day + 1)
cost_with_seven_day = pass_costs[1] + calculate_minimum_cost(current_day + 7)
cost_with_thirty_day = pass_costs[2] + calculate_minimum_cost(current_day + 30)
return min(cost_with_one_day, cost_with_seven_day, cost_with_thirty_day)
return calculate_minimum_cost(1)To find the cheapest way to pay for travel days, we use a smart planning strategy that looks at every single day of the year in order. Instead of getting overwhelmed by future travel, we figure out the best total cost for each day by looking back at the cheapest passes we could have bought previously.
Here's how the algorithm would work step-by-step:
def min_cost_tickets(travel_days, ticket_costs):
last_travel_day = travel_days[-1]
minimum_cost_table = [0] * (last_travel_day + 1)
travel_days_set = set(travel_days)
for current_day in range(1, last_travel_day + 1):
# If no travel is needed today, carry over the exact cost from yesterday.
if current_day not in travel_days_set:
minimum_cost_table[current_day] = minimum_cost_table[current_day - 1]
else:
# Compare single-day, seven-day, and thirty-day passes to find the lowest cost.
cost_with_one_day_pass = minimum_cost_table[current_day - 1] + ticket_costs[0]
cost_with_seven_day_pass = minimum_cost_table[max(0, current_day - 7)] + ticket_costs[1]
cost_with_thirty_day_pass = minimum_cost_table[max(0, current_day - 30)] + ticket_costs[2]
minimum_cost_table[current_day] = min(cost_with_one_day_pass, cost_with_seven_day_pass, cost_with_thirty_day_pass)
# The final value reveals the absolute minimum amount of money needed for all travels.
return minimum_cost_table[last_travel_day]| Case | How to Handle |
|---|---|
| Empty days array where no travel is planned | Return zero cost immediately since no travel passes are required. |
| Single travel day in the days array | The algorithm will correctly compare the costs of all three pass types and choose the minimum single value. |
| Maximum day value up to 365 with dense travel days | A dynamic programming approach of fixed size 366 scales efficiently and computes the result in constant time relative to the year. |
| Travel days are far apart and separated by more than thirty days | The recurrence relation will independently evaluate purchasing a 1-day pass for each isolated travel day as the optimal choice. |
| Pass costs are heavily skewed where a 30-day pass is cheaper than a 1-day pass | The minimization logic naturally selects the longer pass to cover multiple future days efficiently. |
| Days array is already sorted versus unsorted input | The algorithm assumes standard chronological order or explicitly sorts the days array at the beginning to ensure correct state transitions. |
| Overlapping passes covering days beyond the final travel day | The DP state gracefully looks up costs beyond the last travel day by clamping indices to zero additional cost. |
| Very large cost values in the costs array causing potential integer overflow | Standard 32-bit or 64-bit integers prevent overflow since maximum possible cost is bounded by 365 times the maximum pass price. |