Skip to content

Commit 4d39357

Browse files
committed
update maxProfit
1 parent 2f50432 commit 4d39357

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

best-time-to-buy-and-sell-stock/jylee2033.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,23 @@ def maxProfit(self, prices: List[int]) -> int:
88
profit = 0
99

1010
# Iterate through prices
11-
for i, price in enumerate(prices):
11+
# for i, price in enumerate(prices):
12+
# if price < buy:
13+
# buy = price
14+
#
15+
# for j in range(i + 1, len(prices)):
16+
# if prices[j] <= buy:
17+
# continue
18+
#
19+
# if prices[j] - buy > profit:
20+
# profit = prices[j] - buy
21+
for price in prices:
1222
if price < buy:
1323
buy = price
14-
15-
for j in range(i + 1, len(prices)):
16-
if prices[j] <= buy:
17-
continue
18-
19-
if prices[j] - buy > profit:
20-
profit = prices[j] - buy
24+
elif price - buy > profit:
25+
profit = price - buy
2126

2227
return profit
2328

24-
# Time Complexity: O(n^2)
29+
# Time Complexity: O(n)
2530
# Space Complexity: O(1)

0 commit comments

Comments
 (0)