Skip to content

Commit 6ebad6e

Browse files
Time: 295 ms (53.35%) | Memory: 32.1 MB (45.85%) - LeetSync
1 parent c81cf8e commit 6ebad6e

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution:
2+
def maxProfit(self, prices: List[int]) -> int:
3+
4+
dp1 = [0] * len(prices)
5+
dp2 = [0] * len(prices)
6+
7+
mi = prices[0]
8+
max_diff = 0
9+
10+
for i in range(len(prices)):
11+
mi = min(mi, prices[i])
12+
max_diff = max(max_diff, prices[i] - mi)
13+
dp1[i] = max_diff
14+
15+
ma = prices[len(prices) - 1]
16+
max_diff = 0
17+
18+
for i in range(len(prices) - 1, -1, -1):
19+
ma = max(ma, prices[i])
20+
max_diff = max(max_diff, ma - prices[i])
21+
dp2[i] = max_diff
22+
23+
m = 0
24+
for i in range(len(prices) - 1):
25+
m = max(m, dp1[i] + dp2[i + 1])
26+
27+
return max(m, dp2[0])

0 commit comments

Comments
 (0)