题目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