File tree Expand file tree Collapse file tree 1 file changed +9
-14
lines changed
best-time-to-buy-and-sell-stock Expand file tree Collapse file tree 1 file changed +9
-14
lines changed Original file line number Diff line number Diff line change @@ -8,23 +8,18 @@ def maxProfit(self, prices: List[int]) -> int:
88 profit = 0
99
1010 # Iterate through 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 :
11+ for i , price in enumerate (prices ):
2212 if price < buy :
2313 buy = price
24- elif price - buy > profit :
25- profit = price - buy
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
2621
2722 return profit
2823
29- # Time Complexity: O(n)
24+ # Time Complexity: O(n^2 )
3025# Space Complexity: O(1)
You can’t perform that action at this time.
0 commit comments