122. Best Time to Buy and Sell Stock II

来源:互联网 发布:淘宝预售发货时间 编辑:程序博客网 时间:2024/06/05 10:51
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if(prices.size()<2) return 0;
        int min=prices[0],maxpro=0,max=0;
        for (int i=1;i<prices.size();i++){ 
            if(prices[i-1]>prices[i]){
            min=prices[i];
            maxpro+=max;
            max=0;
            }
            else{
                if(max<prices[i]-min)
                max=prices[i]-min;
            }
        }
        return maxpro+max;
    }
};
/*class Solution {
public:
    int maxProfit(vector &a) {
        int profit = 0;
        for (int i = 1; i < a.size(); ++i) {
            if (a[i] > a[i-1]) {
                profit += a[i]-a[i-1];
            }
        }
        return profit;
    }
};*/
0 0
原创粉丝点击