-
Notifications
You must be signed in to change notification settings - Fork 0
/
193.py
55 lines (44 loc) · 1.53 KB
/
193.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
47
48
49
50
51
52
53
54
55
"""
Problem:
Given a array of numbers representing the stock prices of a company in chronological
order, write a function that calculates the maximum profit you could have made from
buying and selling that stock. You're also given a number fee that represents a
transaction fee for each buy and sell transaction.
You must buy before you can sell the stock, but you can make as many transactions as
you like.
For example, given [1, 3, 2, 8, 4, 10] and fee = 2, you should return 9, since you
could buy the stock at $1, and sell at $8, and then buy it at $4 and sell it at $10.
Since we did two transactions, there is a $4 fee, so we have 7 + 6 = 13 profit minus $4
of fees.
"""
from typing import List
def get_max_profit(
prices: List[int], fee: int, profit: int = 0, current: int = 0, can_buy: bool = True
) -> int:
if not prices:
return profit
if can_buy:
return max(
get_max_profit(
prices[1:], fee, profit, (-prices[0] - fee), False
), # buying
get_max_profit(
prices[1:], fee, profit, 0, True
), # holding
)
return max(
get_max_profit(
prices[1:], fee, (profit + current + prices[0]), 0, True
), # selling
get_max_profit(
prices[1:], fee, profit, current, False
), # holding
)
if __name__ == "__main__":
print(get_max_profit([1, 3, 2, 8, 4, 10], 2))
print(get_max_profit([1, 3, 2, 1, 4, 10], 2))
"""
SPECS:
TIME COMPLEXITY: O(2 ^ n)
SPACE COMPLEXITY: O(n)
"""