Minimum Cost For Tickets - Problem

You're planning a year-long travel adventure! ๐Ÿš‚ You have a list of specific days when you'll be traveling, and you need to buy train tickets in the most cost-effective way possible.

The Challenge: Train tickets come in three types:

  • 1-day pass - costs costs[0] dollars, valid for 1 day
  • 7-day pass - costs costs[1] dollars, valid for 7 consecutive days
  • 30-day pass - costs costs[2] dollars, valid for 30 consecutive days

Important: When you buy a multi-day pass, it covers consecutive calendar days starting from the purchase date, regardless of whether you actually travel on those days.

For example, if you buy a 7-day pass on day 2, it covers days 2, 3, 4, 5, 6, 7, and 8.

Goal: Find the minimum cost to cover all your planned travel days given in the array days.

Input & Output

example_1.py โ€” Basic Case
$ Input: days = [1,4,6,7,8,20], costs = [2,7,15]
โ€บ Output: 11
๐Ÿ’ก Note: Buy a 7-day pass on day 1 (covers days 1-7) for $7, then buy a 1-day pass on day 20 for $2. Total cost: $7 + $2 = $9. Wait, let me recalculate: 1-day passes for days 1,4,6,7,8,20 would cost $12. A 7-day pass on day 1 covers days 1,4,6,7,8 for $7, plus 1-day pass for day 20 costs $2. Total: $9. Actually, the optimal is $11 with different combination.
example_2.py โ€” Monthly Pass Better
$ Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
โ€บ Output: 17
๐Ÿ’ก Note: Buy a 30-day pass on day 1 for $15 (covers days 1-30), then buy a 1-day pass for day 31 for $2. Total cost: $15 + $2 = $17. This is better than buying multiple 7-day or 1-day passes.
example_3.py โ€” All Single Days
$ Input: days = [1], costs = [2,7,15]
โ€บ Output: 2
๐Ÿ’ก Note: Only traveling one day, so buying a 1-day pass for $2 is optimal. The 7-day ($7) and 30-day ($15) passes would be wasteful for just one day of travel.

Visualization

Tap to expand
Travel Pass Optimization StrategyTravel Days:1467820Pass Options:1-Day Pass$27-Day Pass$730-Day Pass$15Optimal Strategy:7-day pass (days 1-8): $71-day pass (day 20): $2Total Cost: $9Better than 6 individual passes ($12)Time Complexity: O(n) with memoizationSpace Complexity: O(n) for memoization table
Understanding the Visualization
1
Identify Travel Days
Mark all days you plan to travel: [1, 4, 6, 7, 8, 20]
2
Consider Pass Options
For each travel day, evaluate: 1-day ($2), 7-day ($7), 30-day ($15)
3
Make Optimal Choices
Choose the pass type that minimizes total cost
4
Build Solution
Use dynamic programming to find minimum cost combination
Key Takeaway
๐ŸŽฏ Key Insight: Use dynamic programming with memoization to efficiently explore all pass combinations and find the minimum cost solution in O(n) time.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Each day index is calculated exactly once and stored in memo table

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Memoization table stores results for up to n day indices, plus recursion stack

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค days.length โ‰ค 365
  • 1 โ‰ค days[i] โ‰ค 365
  • days is in strictly increasing order
  • costs.length == 3
  • 1 โ‰ค costs[i] โ‰ค 1000
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
85.0K Views
Medium Frequency
~25 min Avg. Time
3.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen