Skip to content

Commit e6bd452

Browse files
author
Your Name
committed
modified_code
1 parent 678dedb commit e6bd452

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

greedy_methods/best_time_to_buy_and_sell_stock.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,18 @@ def max_profit(prices: list[int]) -> int:
2626
return 0
2727

2828
min_price = prices[0]
29-
max_profit: int = 0
29+
max_profit_value = 0
3030

31-
for price in prices:
32-
min_price = min(price, min_price)
33-
max_profit = max(price - min_price, max_profit)
31+
for price in prices[1:]: # start from second element
32+
if price < min_price:
33+
min_price = price
34+
else:
35+
max_profit_value = max(max_profit_value, price - min_price)
3436

35-
return max_profit
37+
return max_profit_value
3638

3739

3840
if __name__ == "__main__":
3941
import doctest
40-
4142
doctest.testmod()
4243
print(max_profit([7, 1, 5, 3, 6, 4]))

0 commit comments

Comments
 (0)