A+B+C 的和等于差值 D 所对应的连续峰和谷的高度之差。
C语言版:
int maxProfit(int* prices, int pricesSize) {
int i;
int profit = 0;
for(i = 0; i < pricesSize - 1; i++){
if(prices[i] < prices[i + 1])
profit += prices[i + 1] - prices[i];
}
return profit;
}
Python版:
class Solution:
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
profit = 0
for i in range(len(prices) - 1):
profit += max(prices[i + 1] - prices[i], 0)
return profit