[LeetCode]题解(python):122-Best Time to Buy and Sell Stock II

题目来源:

  https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/


 

题意分析:

  和上题类似,给定array,代表第i天物品i的价格。如果可以交易无数次(手上有物品不能买),问最高利润。


 

题目思路:

  记录当前最小值,如果遇到array[i]  < min,那么加上当前的最大值;更新min。


 

代码(python):

  

class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        if len(prices) == 0:
            return 0
        ans,mins = 0,prices[0]
        for i in prices:
            if i > mins:
                ans += i - mins
            mins = i
        return ans
View Code

 

posted @ 2016-03-21 16:48  Ry_Chen  阅读(568)  评论(0编辑  收藏  举报