https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/submissions/
观察知,累加每两个相邻峰谷之间的差价即可
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
i, peak, valley, max_profit = 1, 0, 0, 0
while i < len(prices):
while i < len(prices) and prices[i] <= prices[i-1]:
i += 1
valley = prices[i-1] #找到谷值
while i < len(prices) and prices[i] >= prices[i-1]:
i += 1
peak = prices[i-1] #找到峰值
max_profit += peak - valley #累加差价
return max_profit