您的位置:首页 > 其它

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

2015-12-10 22:44 441 查看
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/

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

参考

/article/4981578.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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: