Skip to content

Commit

Permalink
feature: best time to buy and sell stock revisit
Browse files Browse the repository at this point in the history
  • Loading branch information
solairerove committed Nov 21, 2023
1 parent 6c66601 commit 5b4ef5a
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions dynamic_programming/BestTimeToBuyAndSellStock.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

# O(n) time || O(1) space
def max_profit(self, prices: List[int]) -> int:
min_price, max_profit = float('inf'), 0
for price in prices:
min_price = min(min_price, price)
max_profit = max(max_profit, price - min_price)
min_price, res = prices[0], 0
for price in prices[1:]:
if price < min_price:
min_price = price
else:
res = max(res, price - min_price)

return max_profit
return res

0 comments on commit 5b4ef5a

Please sign in to comment.