Skip to content

Commit be45db2

Browse files
committed
Add Solution.py for 0123.Best Time to Buy and Sell Stock III
1 parent efd1d5d commit be45db2

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def maxProfit(self, prices: List[int]) -> int:
3+
length = len(prices)
4+
if 0 == length:
5+
return 0
6+
dp = [([0] * length) for i in range(3)]
7+
for i in range(1, 3, 1):
8+
maxdiff = -((1 << 31) - 1)
9+
for j in range(1, length, 1):
10+
maxdiff = max(maxdiff, dp[i-1][j-1] - prices[j-1])
11+
dp[i][j] = max(dp[i][j - 1], maxdiff + prices[j])
12+
return dp[2][length - 1]

β€Žsolution/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,8 @@
488488
β”œβ”€β”€ 0123.Best Time to Buy and Sell Stock III
489489
β”‚Β Β  β”œβ”€β”€ README.md
490490
β”‚Β Β  β”œβ”€β”€ Solution.cpp
491-
β”‚Β Β  └── Solution.java
491+
β”‚Β Β  β”œβ”€β”€ Solution.java
492+
β”‚Β Β  └── Solution.py
492493
β”œβ”€β”€ 0124.Binary Tree Maximum Path Sum
493494
β”‚Β Β  └── Solution.java
494495
β”œβ”€β”€ 0125.Valid Palindrome

0 commit comments

Comments
Β (0)