-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path23.py
More file actions
18 lines (16 loc) · 654 Bytes
/
23.py
File metadata and controls
18 lines (16 loc) · 654 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution:
def maxProfit(self, k: int, prices: List[int]) -> int:
n = len(prices)
nexxt = [[0]*(k+1) for _ in range(2)]
cur = [[0]*(k+1) for _ in range(2)]
for i in range(n-1, -1, -1):
for j in range(2):
for transc in range(k):
profit = 0
if j:
profit = max(nexxt[0][transc] - prices[i], nexxt[1][transc])
else:
profit = max(nexxt[1][transc+1] + prices[i], nexxt[0][transc])
cur[j][transc] = profit
nexxt = cur
return cur[1][0]