122. Best Time to Buy and Sell Stock II

核心思路:只要当日价格比前一日高,就可以视作一次卖出,结算收益。

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        prev = float("inf")
        res = 0
        for p in prices:
            if p > prev: res += p-prev
            prev = p
        return res

Last updated

Was this helpful?