问题描述
题解
代码
int maxProfit(int* prices, int pricesSize){
if(pricesSize<=1){
return 0;
}
int ans=0,start=prices[0],end_=prices[0];
for(int i=1;i<pricesSize;i++){
if(prices[i]>=end_){
end_=prices[i];
}
else{
int temp=(end_>start?end_-start:0);
ans+=temp;
start=end_=prices[i];
}
}
ans+=(end_>start?end_-start:0);
return ans;
}
别人的代码
int maxProfit(int* prices, int pricesSize) {
if(pricesSize<=0) return 0;
int count=0,k;
for(int i=1;i<pricesSize;i++){
k=prices[i]-prices[i-1];
if(k>0) count+=k;
}
return count;
}