From 8f564ca026b4915087dea87de348a730d184ef4e Mon Sep 17 00:00:00 2001 From: Samir Paul Date: Mon, 4 Apr 2022 16:22:25 +0530 Subject: [PATCH] dp --- .../11. Minimum Cost For Tickets.py | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 02_Dynamic-Programming/12. DP Using 1D Array/11. Minimum Cost For Tickets.py diff --git a/02_Dynamic-Programming/12. DP Using 1D Array/11. Minimum Cost For Tickets.py b/02_Dynamic-Programming/12. DP Using 1D Array/11. Minimum Cost For Tickets.py new file mode 100644 index 00000000..dcf85978 --- /dev/null +++ b/02_Dynamic-Programming/12. DP Using 1D Array/11. Minimum Cost For Tickets.py @@ -0,0 +1,26 @@ +# https://leetcode.com/problems/minimum-cost-for-tickets/ + +class Solution: + def mincostTickets(self, days: List[int], costs: List[int]) -> int: + n = max(days) + 1 + dp = [-1 for x in range(n)] + #dp = [-1]*(max(days)+1) + dp[0] = 0 + + for x in range(1,len(dp)): + if x in days: + if x>=30: + #print(x,min(dp[x-1]+costs[0],dp[x-7]+costs[1],dp[x-30]+costs[2])) + dp[x] = min(dp[x-1]+costs[0],dp[x-7]+costs[1],dp[x-30]+costs[2]) + + elif x>=7 and x<30: + dp[x] = min(dp[x-1]+costs[0],dp[x-7]+costs[1],costs[2]) + + else: + dp[x] = min(dp[x-1]+costs[0],costs[1],costs[2]) + + else: + dp[x] = dp[x-1] + + return dp[max(days)] + \ No newline at end of file