LeetCode122 买卖股票的最佳时机 II(贪心)

2022/6/29 23:21:33

本文主要是介绍LeetCode122 买卖股票的最佳时机 II(贪心),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

LeetCode122 买卖股票的最佳时机 II

贪心计算爬峰收益

class Solution:
    def maxProfit(self, prices: List[int]) -> int:

        ans, l = 0, len(prices)
        for i in range(1, l): ans += max(0, prices[i] - prices[i - 1])

        return ans


这篇关于LeetCode122 买卖股票的最佳时机 II(贪心)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程