-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBestTimeToBuyAndSellStockIII_v0.py
More file actions
48 lines (39 loc) · 1.09 KB
/
BestTimeToBuyAndSellStockIII_v0.py
File metadata and controls
48 lines (39 loc) · 1.09 KB
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
#!/usr/bin/env python
# encoding: utf-8
class Solution:
# @param {integer[]} prices
# @return {integer}
def maxProfit(self, prices):
if not prices:
return 0
max_p = 0
for i in range(len(prices)):
l = self._maxProfit(prices[:i])
r = self._maxProfit(prices[i:])
v = l + r
if max_p < v:
max_p = v
return max_p
def _maxProfit(self, prices):
max_p = -1
min_p = 2**31
max_profit = 0
for i in range(len(prices)):
p = prices[i]
if min_p > p:
min_p = p
max_p = -1
if p > max_p:
max_p = p
diff = p - min_p
if max_profit < diff:
max_profit = diff
return max_profit
if __name__ == '__main__':
s = Solution()
print s.maxProfit([4,1,2,5,7,9,3,5])
print s.maxProfit([1])
print s.maxProfit([1,2])
print s.maxProfit([1,4,2])
print s.maxProfit([3,2,6,5,0,3])
print s.maxProfit([1,2,4,2,5,7,2,4,9,0])