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 Jan 2, 2024
1 parent b6cd486 commit 0ed7cb5
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions dynamic_programming/BestTimeToBuyAndSellStock.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

# O(n) time || O(1) space
def max_profit(self, prices: List[int]) -> int:
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)
min_price, max_profit = prices[0], 0
for i in range(1, len(prices)):
min_price = min(min_price, prices[i])
max_profit = max(max_profit, prices[i] - min_price)

return res
return max_profit


# O(n) time || O(1) space
Expand Down

0 comments on commit 0ed7cb5

Please sign in to comment.