983. Minimum Cost For Tickets
目標 dp[i] 是當在旅程從第 i 天開始,完成所有旅程所需要的最少成本。
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)