思路:因为要求的是最大收益,所以可以假设每天都买,第二天卖 利用一个数组来统计,如果收益为负则ans[i]=0,否则ans[i]=prices[i+1]-prices[i]
code:
class Solution {public int maxProfit(int[] prices) {int n=prices.length;int ans[]=new int[n-1];for(int i=0;i<n-1;i++){if(prices[i+1]>prices[i])ans[i]=prices[i+1]-prices[i];elseans[i]=0;}int res=0;for(int i=0;i<n-1;i++){res+=ans[i];}return res;}
}