Skip to content

Commit 7e37d94

Browse files
committed
best time to buy and sell stock solution
1 parent f93ae6c commit 7e37d94

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# time complexity : O(n)
2+
# space complexity : O(1)
3+
from typing import List
4+
5+
6+
class Solution:
7+
def maxProfit(self, prices: List[int]) -> int:
8+
n = len(prices)
9+
min_price = prices[0]
10+
max_profit = 0
11+
12+
for i in range(1, n):
13+
min_price = min(min_price, prices[i])
14+
max_profit = max(max_profit, prices[i] - min_price)
15+
16+
return max_profit

0 commit comments

Comments
 (0)