We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 466f3be commit 00dd7c7Copy full SHA for 00dd7c7
1 file changed
best-time-to-buy-and-sell-stock/jylee2033.py
@@ -8,18 +8,20 @@ def maxProfit(self, prices: List[int]) -> int:
8
profit = 0
9
10
# Iterate through prices
11
- for i, price in enumerate(prices):
12
- if price < buy:
13
- buy = price
+ for price in prices:
+ # if price < buy:
+ # buy = price
14
15
- for j in range(i + 1, len(prices)):
16
- if prices[j] <= buy:
17
- continue
+ # for j in range(i + 1, len(prices)):
+ # if prices[j] <= buy:
+ # continue
18
19
- if prices[j] - buy > profit:
20
- profit = prices[j] - buy
+ # if prices[j] - buy > profit:
+ # profit = prices[j] - buy
21
+ buy = min(buy, price)
22
+ profit = max(profit, price - buy)
23
24
return profit
25
-# Time Complexity: O(n^2)
26
+# Time Complexity: O(n)
27
# Space Complexity: O(1)
0 commit comments