forked from y-ncao/Python-Study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBest_Time_to_Buy_and_Sell_Stock_III.py
36 lines (32 loc) · 1.11 KB
/
Best_Time_to_Buy_and_Sell_Stock_III.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
"""
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most two transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
"""
class Solution:
# @param prices, a list of integer
# @return an integer
def maxProfit(self, prices):
N = len(prices)
if N <= 1:
return 0
dp_1 = [0 for i in range(N)]
dp_2 = [0 for i in range(N)]
min_price = prices[0]
i = 1
while i < N:
min_price = min(min_price, prices[i])
dp_1[i] = max(dp_1[i-1], prices[i]-min_price)
i+= 1
max_price = prices[-1]
i = N-2
while i >= 0:
max_price = max(max_price, prices[i])
dp_2[i] = max(dp_2[i+1], max_price-prices[i])
i -= 1
res = 0
for i in range(N):
res = max(res, dp_1[i] + dp_2[i])
return res
# Very similart to trapping rain water, from left to right then right to left