题目:买卖股票的最佳时机 II
C语言解题
int maxProfit(int* prices, int pricesSize) {
int i, profit=0;
if (pricesSize == 0)
{
return 0;
}
for (i = 1; i < pricesSize; i++)
{
if (prices[i] > prices[i - 1])
{
profit += prices[i] - prices[i - 1];
}
}
return profit;
}
遍历数组,只要相邻两个数的差值为正,就加入到profit中,最后的和即为最大利润。