
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
股票买卖最大利润算法

本文介绍了一种计算股票买卖最大利润的算法,通过遍历价格数组,比较相邻两天的价格差,若后一天价格高于前一天,则将差价累加到总利润中。提供了C语言和Python两种实现方式。
3890

被折叠的 条评论
为什么被折叠?



