python实现leetcode之122. 买卖股票的最佳时机

2021-10-04  本文已影响0人  深圳都这么冷

解题思路

由于可以交易任意多次
先计算一天的利润
然后将大于0的利润相加即可

122. 买卖股票的最佳时机 II

代码

class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        profits = [x-y for x, y in zip(prices[1:], prices)]
        return sum(p for p in profits if p>0)
效果图
上一篇 下一篇

猜你喜欢

热点阅读