Skip to content

Commit f1aa488

Browse files
committed
add solution for best time to buy and sell stock problem
1 parent 8a4a45d commit f1aa488

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
# Approach
3+
지금까지의 최저 가격을 갱신함과 동시에 최선의 이익도 업데이트합니다.
4+
5+
# Complexity
6+
- Time complexity: O(N)
7+
8+
- Space complexity: O(1)
9+
"""
10+
11+
12+
class Solution:
13+
def maxProfit(self, prices: list[int]) -> int:
14+
min_price = float("inf")
15+
answer = 0
16+
17+
for price in prices:
18+
min_price = min(min_price, price)
19+
answer = max(answer, price - min_price)
20+
21+
return answer

0 commit comments

Comments
 (0)