Skip to content

Commit 1a65dab

Browse files
Best Time To Buy and Sell Stock
1 parent 9131957 commit 1a65dab

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

0121-Best-Time-To-Buy-and-Sell-Stock.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,16 @@ def maxProfit(self, prices: List[int]) -> int:
3838
else:
3939
buy = sell
4040
sell += 1
41-
return max_profit
41+
return max_profit
42+
43+
# Kadane's Algorithm
44+
45+
class Solution:
46+
def maxProfit(self, prices: List[int]) -> int:
47+
profit = max_profit = 0
48+
49+
for i in range(1, len(prices)):
50+
profit = max(0, profit + prices[i] - prices[i-1])
51+
max_profit = max(max_profit, profit)
52+
53+
return max_profit

0 commit comments

Comments
 (0)