LeetCode solutions

1025. Minimum Cost For Tickets

My accepted Python solution to LeetCode problem 1025, Minimum Cost For Tickets, running in 4ms.

  • Difficulty: Medium
  • Python
  • Runtime 4ms
  • Memory 17.3MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 4ms, memory 17.3MB, accepted 2026-01-02.

python
class Solution:
    def mincostTickets(self, days: List[int], costs: List[int]) -> int:
        day_set = set(days)
        max_day = days[-1]
        
        # dp[i] = min cost to travel up to day i
        dp = [0] * (max_day + 1)
        
        for i in range(1, max_day + 1):
            if i not in day_set:
                dp[i] = dp[i - 1]
            else:
                dp[i] = min(
                    dp[i - 1] + costs[0],
                    dp[max(0, i - 7)] + costs[1],
                    dp[max(0, i - 30)] + costs[2]
                )
        
        return dp[max_day]

Source