Skip to content

Commit 72f3ad0

Browse files
authored
Create minimum-cost-for-tickets.py
1 parent 2f66f8e commit 72f3ad0

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Python/minimum-cost-for-tickets.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Time: O(n)
2+
# space: O(n)
3+
4+
class Solution(object):
5+
def mincostTickets(self, days, costs):
6+
"""
7+
:type days: List[int]
8+
:type costs: List[int]
9+
:rtype: int
10+
"""
11+
dp = [float('inf') for i in xrange(len(days)+1)]
12+
dp[0] = 0
13+
last_buy_days = [0, 0, 0]
14+
durations = [1, 7, 30]
15+
for i in xrange(1,len(days)+1):
16+
for j in xrange(len(durations)):
17+
while i-1 < len(days) and \
18+
days[i-1] > days[last_buy_days[j]]+durations[j]-1:
19+
last_buy_days[j] += 1
20+
dp[i] = min(dp[i], dp[last_buy_days[j]]+costs[j])
21+
return dp[-1]

0 commit comments

Comments
 (0)