Skip to content

Commit 4f483fc

Browse files
committed
Simplify code for 121.
1 parent 17b951d commit 4f483fc

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

121 Best Time to Buy and Sell Stock.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@ def maxProfit(self, prices):
1212
"""
1313
if len(prices) < 2:
1414
return 0
15-
max_price = min_price = prices[0]
15+
min_price = prices[0]
1616
max_profit = 0
1717
for price in prices:
18-
if price > max_price:
19-
max_price = price
2018
if price < min_price:
21-
max_price = min_price = price
22-
if max_price - min_price > max_profit:
23-
max_profit = max_price - min_price
19+
min_price = price
20+
if price - min_price > max_profit:
21+
max_profit = price - min_price
2422
return max_profit
2523

2624

0 commit comments

Comments
 (0)