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
思路:
假设把数组中的数据可视化:
从图中可以看到A+B+C = D
它的意义在于连续的差值之和与全局最大的差值是一致的。