Skip to content

Commit 3ac2ea0

Browse files
committed
best time to buy and sell stock solution
1 parent 8c34ca2 commit 3ac2ea0

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# 1) Keep track of local minimum and use local minimum to update max profile while interating prices.
2+
# TC: O(N) where N is the length of prices
3+
# SC: O(1)
4+
class Solution:
5+
def maxProfit(self, prices: List[int]) -> int:
6+
min_price = prices[0]
7+
max_profit = 0
8+
9+
for price in prices:
10+
max_profit = max(max_profit, price - min_price)
11+
min_price = min(min_price, price)
12+
13+
return max_profit

0 commit comments

Comments
 (0)