【贪心】see-122. Best Time to Buy and Sell Stock II

贪心算法求解:

class Solution:
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        if prices == [] or len(prices) == 1:
            return 0
        
        ans = 0
        minPrice = prices[0]
        for item in prices:
            if minPrice > item:
                minPrice = item
            else:
                ans += (item-minPrice)
                minPrice = item
        return ans
                    

 


版权声明:本文为leetcodecl原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。