Scribbling

LeetCode: 122. Best Time to Buy and Sell Stock II 본문

Computer Science/Coding Test

LeetCode: 122. Best Time to Buy and Sell Stock II

focalpoint 2021. 11. 3. 20:20
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        profit, prev = 0, prices[0]
        for i in range(1, len(prices)):
            if prices[i] >= prev:
                profit += prices[i] - prev
            prev = prices[i]
        return profit