最长连续递增

来源:互联网 发布:最流行的网络歌曲2017 编辑:程序博客网 时间:2024/05/21 06:55


class Solution {
public:
int maxProfit(vector<int> &prices) {
int n = prices.size() ;
int start = 0,end = 0 ;
int profit = 0 ;
int maxProfit = 0 ;
int minStart = 0 ; 
if(prices.size() <= 1)
return 0;


if(prices[0] <= prices[1])
{
start = 0 ; end =1;
maxProfit = prices[1] - prices[0];
}
else
{
start = 1;end = 1;
}
while (start <= n-2)
{
while (end <= n-1 && prices[end]>= prices[end-1])
{
profit += prices[end]- prices[end-1];
end++ ;
}
if(profit > maxProfit){
minStart = start ;
maxProfit = profit ;
profit = 0 ;
}
while(end <= n-1 && prices[end] <= prices[end-1])
{
end ++ ;
}
start = end - 1;
}
return maxProfit;
}
};
0 0
原创粉丝点击