Skip to content

Commit 7c6c464

Browse files
committed
solve best time to buy and sell stock
1 parent b9449de commit 7c6c464

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
# Time Complexity: O(n), n: len(prices)
3+
# Space Complexity: O(1)
4+
def maxProfit(self, prices: List[int]) -> int:
5+
min_price = prices[0]
6+
profit = 0
7+
8+
for i in range(1, len(prices)):
9+
min_price = min(min_price, prices[i])
10+
profit = max(profit, prices[i] - min_price)
11+
12+
return profit

0 commit comments

Comments
 (0)