
122. Best Time to Buy and Sell Stock II
·
Algorithm
1. Greedy class Solution: def maxProfit(self, prices: List[int]) -> int: stack = [] answer = 0 for price in prices: if not stack: stack.append(price) else: if stack[-1] < price: answer+=price-stack[-1] stack.pop() stack.append(price) else: stack.pop() stack.append(price) return answer 주어진 날짜에 대한 주식 가격 목록에서 주식을 사고 팔 최댓값을 반환하면 된다. 그리디 하게 생각해서 해결할 수 있는 문제다. [ 1, 3, 7 ] 첫째, 둘째, 셋째 날 주식의 가격이 위와 같다고 가..