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