LeetCode 122

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if(prices.size() < 2) return 0;
        int max_profit = 0;
        for(int i = 1;i < prices.size(); ++i){
            int tmp = prices[i] - prices[i-1];
            if(tmp > 0)
                max_profit += tmp;
        }
        return max_profit;
    }
};

只要上涨就加上。

posted on 2019-03-27 11:11  庭中核桃树  阅读(117)  评论(0编辑  收藏  举报

导航