Skip to content

Commit d1580f8

Browse files
committed
best-time-to-buy-and-sell-stock solution
1 parent 563bc02 commit d1580f8

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
TC : O(N)
3+
for문 한 번 => O(N)
4+
5+
SC : O(1)
6+
변수 3개 선언 이외에 추가적으로 사용하는 메모리 없으므로
7+
"""
8+
9+
class Solution:
10+
def maxProfit(self, prices: List[int]) -> int:
11+
max_profit = 0
12+
buy = prices[0]
13+
sell = prices[0]
14+
for price in prices:
15+
if price < buy:
16+
buy = price
17+
sell = price
18+
if price > sell:
19+
sell = price
20+
max_profit = max(max_profit, sell - buy)
21+
return max_profit

0 commit comments

Comments
 (0)