-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
31 lines (27 loc) · 901 Bytes
/
solution.py
File metadata and controls
31 lines (27 loc) · 901 Bytes
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
class Solution(object):
def maxProfit(self, prices, fee):
"""
:type prices: List[int]
:type fee: int
:rtype: int
"""
if len(prices) == 0:
return 0
holder = prices[0]
max_price = prices[0]
max_profit = 0
for i in xrange(1, len(prices)):
price = prices[i]
if price > max_price:
max_price = price
elif max_price - price >= fee:
if max_price - holder - fee > 0:
max_profit += max_price - holder - fee
holder = price
max_price = price
elif price < holder:
holder = price
max_price = price
if max_price - holder > fee:
max_profit += max_price - holder - fee
return max_profit