-
Notifications
You must be signed in to change notification settings - Fork 892
/
problem_130.py
35 lines (29 loc) · 1.12 KB
/
problem_130.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
def profit_helper(prices, curr_index, curr_profit, buys_left, sells_left):
if curr_index == len(prices) or not sells_left:
# reached end of chronology or exhausted trades
return curr_profit
if buys_left == sells_left:
# buy or wait
return max(
# buy
profit_helper(prices, curr_index + 1, curr_profit - prices[curr_index],
buys_left - 1, sells_left),
# wait
profit_helper(prices, curr_index + 1, curr_profit,
buys_left, sells_left)
)
else:
# sell or hold
return max(
# sell
profit_helper(prices, curr_index + 1, curr_profit + prices[curr_index],
buys_left, sells_left - 1),
# hold
profit_helper(prices, curr_index + 1, curr_profit,
buys_left, sells_left)
)
def get_max_profit(prices, k):
return profit_helper(prices, 0, 0, k, k)
assert get_max_profit([5, 2, 4, 0, 1], 2) == 3
assert get_max_profit([5, 2, 4], 2) == 2
assert get_max_profit([5, 2, 4], 1) == 2