題目python
class Solution: def maxProfit(self, prices): """ :type prices: List[int] :rtype: int """ profit = 0 for i in range(1,len(prices)): if prices[i] - prices[i-1] > 0: profit += prices[i] - prices[i-1] return profit
思路:數組
假設把數組中的數據可視化:spa
從圖中能夠看到A+B+C = Dcode
它的意義在於連續的差值之和與全局最大的差值是一致的。blog