Skip to content

Commit

Permalink
Re-solve "Best Time to Buy and Sell Stock"
Browse files Browse the repository at this point in the history
  • Loading branch information
lancelote committed Feb 6, 2024
1 parent 69e9e4d commit dccc0e3
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/best_time_to_buy_and_sell_stock.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
class Solution:
def maxProfit(self, prices: list[int]) -> int:
left = 0
right = 0
assert prices

min_price = prices[0]
max_profit = 0

while right < len(prices):
current_profit = prices[right] - prices[left]
max_profit = max(max_profit, current_profit)
if current_profit < 0:
left = right
right += 1
for price in prices:
max_profit = max(max_profit, price - min_price)
min_price = min(min_price, price)

return max_profit

0 comments on commit dccc0e3

Please sign in to comment.