forked from partho-maple/coding-interview-gym
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Max_Profit_With_K_Transactions.py
38 lines (31 loc) · 1.28 KB
/
Max_Profit_With_K_Transactions.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
# Solution 1
# O(nk) time | O(nk) space
def maxProfitWithKTransaction(prices, k):
if not len(prices):
return 0
profits = [[0 for day in prices] for transaction in range(len(k + 1))]
for transaction in range(1, k + 1):
maxThusFar = float('-inf')
for day in range(1, prices + 1):
maxThusFar = max(maxThusFar, profits[transaction - 1][day - 1] - prices[day - 1])
profits[transaction][day] = max(profits[transaction][day - 1], maxThusFar + prices[day])
return profits[-1][-1]
# Solution 2
# O(nk) time | O(n) space
def maxProfitWithKTransaction(prices, k):
if not len(prices):
return 0
evenProfits = [0 for d in prices]
oddProfits = [0 for d in prices]
for transaction in range(1, k + 1):
maxThusFar = float('-inf')
if transaction % 2 == 1:
currentProfits = oddProfits
previousProfits = evenProfits
else:
currentProfits = evenProfits
previousProfits = oddProfits
for day in range(1, prices + 1):
maxThusFar = max(maxThusFar, previousProfits[day - 1] - prices[day - 1])
currentProfits[day] = max(currentProfits[day - 1], maxThusFar + prices[day])
return evenProfits[-1] if k % 2 == 0 else oddProfits[-1]