---
title: "983. Minimum Cost For Tickets"
url: "https://laigary.com/interview/coding/983-minimum-cost-for-tickets"
type: "note"
section: "coding"
date: "2023-01-28"
updated: "2026-07-27"
tags: ["Dynamic Programming"]
---

# 983. Minimum Cost For Tickets

[983\. Minimum Cost For Tickets](https://leetcode.com/problems/minimum-cost-for-tickets/)

目標 `dp[i]` 是當在旅程從第 `i` 天開始，完成所有旅程所需要的最少成本。

```python
class Solution:
    def mincostTickets(self, days: List[int], costs: List[int]) -> int:
        dayset = set(days)
        m = max(days)
        costTable = {
            1: costs[0],
            7: costs[1],
            30: costs[2]
        }
        @cache
        def dp(day):
            if day > m:
                return 0
            if day in dayset:
                return min(dp(day + key) + val for key, val in costTable.items())
            return dp(day + 1)
        return dp(1)
```
