122. 买卖股票的最佳时机 II

如果想到其实最终利润是可以分解的,那么本题就很容易了!

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