Skip to content

Commit dccc0e3

Browse files
committedFeb 6, 2024
Re-solve "Best Time to Buy and Sell Stock"
1 parent 69e9e4d commit dccc0e3

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed
 
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
class Solution:
22
def maxProfit(self, prices: list[int]) -> int:
3-
left = 0
4-
right = 0
3+
assert prices
4+
5+
min_price = prices[0]
56
max_profit = 0
67

7-
while right < len(prices):
8-
current_profit = prices[right] - prices[left]
9-
max_profit = max(max_profit, current_profit)
10-
if current_profit < 0:
11-
left = right
12-
right += 1
8+
for price in prices:
9+
max_profit = max(max_profit, price - min_price)
10+
min_price = min(min_price, price)
1311

1412
return max_profit

0 commit comments

Comments
 (0)
Please sign in to comment.