Skip to content

Commit 452da52

Browse files
committed
[W5]
best time to buy and sell stock solution
1 parent 52ac160 commit 452da52

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+
"""
2+
TC: O(n)
3+
SC: O(1)
4+
"""
5+
class Solution:
6+
def maxProfit(self, prices: List[int]) -> int:
7+
max_profit= 0
8+
l = 0
9+
r = 1
10+
11+
while r < len(prices):
12+
if prices[l] < prices[r]:
13+
profit = prices[r] - prices[l]
14+
max_profit = max(max_profit, profit)
15+
16+
else:
17+
l = r
18+
r +=1
19+
return max_profit
20+
21+

0 commit comments

Comments
 (0)