Skip to content

Commit 76e5ace

Browse files
committed
feat: add best time to buy and sell stock in python
1 parent 8b7590a commit 76e5ace

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import List
2+
3+
# Best Time to Buy and Sell Stock
4+
def maxProfit(prices: List[int]) -> int:
5+
maxProfit = 0
6+
l, r = 0, 1
7+
8+
while r < len(prices):
9+
if prices[l] < prices[r]:
10+
profit = prices[r] - prices[l]
11+
maxProfit = max(profit, maxProfit)
12+
else:
13+
l +=1
14+
15+
r +=1
16+
17+
return maxProfit
18+
19+
20+
assert maxProfit([7,1,5,3,6,4]) == 5
21+
assert maxProfit([7,6,4,3,1]) == 0

0 commit comments

Comments
 (0)