From c4f0fa697f77a1b9ea5b2c9f4353e03d323ec680 Mon Sep 17 00:00:00 2001 From: jihyup-byun Date: Fri, 23 Oct 2026 11:32:57 +0000 Subject: [PATCH] 121.2 --- ...121. Best Time to Buy and Sell Stock.2.py" | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 "leetcode3/\353\263\200\354\247\200\355\230\221/v3/121. Best Time to Buy and Sell Stock.2.py" diff --git "a/leetcode3/\353\263\200\354\247\200\355\230\221/v3/121. Best Time to Buy and Sell Stock.2.py" "b/leetcode3/\353\263\200\354\247\200\355\230\221/v3/121. Best Time to Buy and Sell Stock.2.py" new file mode 100644 index 00000000..e50d54ff --- /dev/null +++ "b/leetcode3/\353\263\200\354\247\200\355\230\221/v3/121. Best Time to Buy and Sell Stock.2.py" @@ -0,0 +1,29 @@ +''' +1. 아이디어 : +- 왼쪽에서부터 순회하며 "그 날까지의 최저가"를 minlst에 누적한다. +- 각 날의 price에서 그 날까지의 최저가를 빼면 "그 날 팔았을 때의 최대 이익"이 된다. +- 그 이익들 중 최댓값이 정답. + +2. 시간복잡도 : +o(n) (최저가 누적 1번 + 이익 계산 1번) + +3. 자료구조/알고리즘 : +- 누적 최솟값(prefix min) / dp + +''' + +class Solution: + def maxProfit(self, prices: List[int]) -> int: + minlst = [] + _min = 10e9 + + for price in prices: + if _min > price: + _min = price + minlst.append(_min) + + ans = [] + for i, price in enumerate(prices): + ans.append(price - minlst[i]) + + return max(ans)