-
Notifications
You must be signed in to change notification settings - Fork 19
/
answer.py
46 lines (36 loc) · 1.35 KB
/
answer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python3
#-------------------------------------------------------------------------------
class Solution:
def maxProfit_as_many_transactions(self, prices):
if not prices:
return 0
profit, prev = 0, prices[0]
for i in range(1, len(prices)):
if prices[i] >= prices[i-1]:
profit = profit + prices[i]-prices[i-1]
return profit
def maxProfit(self, k, prices):
"""
:type k: int
:type prices: List[int]
:rtype: int
"""
if len(prices) <= 1 or k == 0: return 0
k = min(k, len(prices)-1)
if k >= len(prices) - 1:
return self.maxProfit_as_many_transactions(prices)
# Create 2d array
maxes = []
for _ in range(k+1):
maxes.append([0]*len(prices))
# Set values in 2d array of max profits
# k -> number of transactions
# i -> the current day
for k in range(1, k+1):
curr = -prices[0]
for i in range(1, len(prices)):
maxes[k][i] = max(maxes[k][i-1], curr + prices[i])
curr = max(curr, maxes[k-1][i-1] - prices[i])
return maxes[k][len(prices)-1]
#-------------------------------------------------------------------------------
# Testing