题目:买卖股票的最佳时机 II
LeetCode初级算法-数组-2LeetCode初级算法-数组-2
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中,最后的和即为最大利润。

相关文章:

  • 2021-04-23
  • 2021-03-27
  • 2021-07-02
  • 2022-12-23
  • 2021-10-20
  • 2021-09-05
  • 2022-12-23
  • 2021-12-29
猜你喜欢
  • 2021-12-06
  • 2021-06-28
  • 2021-08-03
  • 2021-06-06
  • 2021-07-09
  • 2021-04-09
  • 2021-05-08
相关资源
相似解决方案