class Solution {
public:
int maxProfit(vector<int> &prices) {
int profit=0;
int n=prices.size();
prices.push_back(0);
int start=-1;
for(int i=0;i<n;i++){
if(prices[i+1]>prices[i]){
if(start==-1){
start=i;
}
}
else if(start!=-1){
profit+=prices[i]-prices[start];
start=-1;
}
}
return profit;
}
};
LeetCode - 122 Best Time to Buy and Sell Stock II
最新推荐文章于 2015-08-16 19:23:30 发布