Skip to content

Commit 7fd29f2

Browse files
committed
best time to buy and sell stock solution
1 parent 2f86595 commit 7fd29f2

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+
# TC: O(N)
2+
# SC: O(1)
3+
class Solution:
4+
def maxProfit(self, prices: List[int]) -> int:
5+
min_until_now = prices[0]
6+
max_profit = 0
7+
8+
for price in prices[1:]:
9+
max_profit = max(max_profit, price - min_until_now)
10+
min_until_now = min(min_until_now, price)
11+
12+
return max_profit
13+

0 commit comments

Comments
 (0)