classSolution{// 贪心算法publicintmaxProfit(int[] prices){int result =0;for(int i =1; i < prices.length; i++){
result += Math.max(0, prices[i]- prices[i -1]);}return result;}}
解题思路使用贪心算法,只要"有赚到钱"就卖出result += Math.max(0, prices[i] - prices[i - 1])这种方法是用来计算最终受益,而不是真正的买卖方案代码class Solution { // 贪心算法 public int maxProfit(int[] prices) { int result = 0; for (int i = 1; i < prices.length; i++) { .