将所有的递增段的增加值叠加起来
class Solution {
public int maxProfit(int[] prices) {
if (prices.length <= 1){
return 0;
}
int pre = 0;
int p = 1;
int maxPro = 0;
int start = 0;
boolean flag = false; //标志当前是否是递增
while(p < prices.length){
if(prices[pre] < prices[p]){
if (flag == false){
flag = true;
start = pre;
}
}else{
if (flag == true){
flag = false;
maxPro = maxPro + (prices[pre] - prices[start]);
}
}
pre++;
p++;
}
if (flag){
maxPro = maxPro + (prices[pre] - prices[start]);
}
return maxPro;
}
}
标签:150,int,maxProfit,面试,经典,prices
From: https://www.cnblogs.com/poteitoutou/p/18004763