class Solution {
public:
int maxProfit(vector<int> &prices)
{
if(prices.empty())
return 0;
int max=0;
int start=prices[0];
int last=prices[0];
int sz = prices.size();
for(int i=1; i<sz; ++i)
{
if(prices[i]>=prices[i-1])
{
last=prices[i];
if(i==sz-1)
max += (last-start);
}
else
{
max += (last-start);
start=prices[i];
last=prices[i];
}
}
return max;
}
};
Best Time to Buy and Sell Stock II
最新推荐文章于 2022-11-16 09:46:40 发布