Skip to content

Commit 3f75a5b

Browse files
committed
121. Best Time to Buy and Sell Stock Solution
1 parent 1d57850 commit 3f75a5b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
# 121. Best Time to Buy and Sell Stock
3+
# O(n) - Two Pointers
4+
class Solution:
5+
def maxProfit(self, prices: list[int]) -> int:
6+
left = 0
7+
right = 1
8+
profit = 0
9+
max_profit = 0
10+
11+
while right < len(prices):
12+
if prices[right] > prices[left]:
13+
profit = prices[right] - prices[left]
14+
max_profit = max(profit, max_profit)
15+
else:
16+
left = right
17+
right += 1
18+
return max_profit

0 commit comments

Comments
 (0)