저점매수 고점매도1 [LeetCode] Best Time to Buy and Sell Stock II python (greedy) class Solution: # @param prices, a list of integer # @return an integer def maxProfit(self, prices): profit = 0 for i in range(len(prices) - 1): profit += max(0, prices[i + 1] - prices[i]) return profit 저점 매수 고점 매도 실제 시장에서는 불가능하지만 지금은 prices가 주어졌기 때문에 loss가 발생하기 전에 팔고 하락하자마자 다시 사면 profit maximization이 가능하기 때문에 i+1 과 i 시점에서의 차이가 0보다 클 경우 모두 더해주면 된다. 출처: github.com/jiapengwen/LeetCode/blob/master/.. 2021. 4. 1. 이전 1 다음