Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions greedy_methods/best_time_to_buy_and_sell_stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ def max_profit(prices: list[int]) -> int:
return 0

min_price = prices[0]
max_profit: int = 0
max_profit_value = 0

for price in prices:
min_price = min(price, min_price)
max_profit = max(price - min_price, max_profit)
for price in prices[1:]: # start from second element
if price < min_price:
min_price = price
else:
max_profit_value = max(max_profit_value, price - min_price)

return max_profit
return max_profit_value


if __name__ == "__main__":
import doctest

doctest.testmod()
print(max_profit([7, 1, 5, 3, 6, 4]))
Loading