Best Time to Buy and Sell Stock II 计算最佳买入卖出时间

这一题计算最佳买入卖出时间,可能是因为自己思路有问题吧,刚开始想的方向好奇怪,看了一下别人的解法才写出来,其实只要把利润全部都加起来就可以得到最大值了,因为可以随意选择买入时间,只要在卖出之前就好。

class Solution {
    public int maxProfit(int[] prices) {
        int profit = 0;
        for(int i = 0;i < prices.length-1;i++){
        	if(prices[i] < prices[i+1]){
        		profit += prices[i+1] - prices[i];
        	}
        }
        return profit;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_42615847/article/details/83020316