leetcode -- Best Time to Buy and Sell Stock II -- 重点注意思路

来源:互联网 发布:个股评测软件 编辑:程序博客网 时间:2024/06/02 05:06

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

因为可以进行无限次交易,并且在下一次buy之前必须已经sell。所以只需要把所有price曲线价格上涨的部分加起来就行。

参考
http://www.cnblogs.com/zuoyuan/p/3765980.html

class Solution(object):    def maxProfit(self, prices):        """        :type prices: List[int]        :rtype: int        """        maxprofit = 0        for i in range(1, len(prices)):            if prices[i] >= prices[i-1]:                maxprofit += prices[i] - prices[i-1]        return maxprofit
0 0
原创粉丝点击