首页 > 代码库 > best-time-to-buy-and-sell-stock-ii

best-time-to-buy-and-sell-stock-ii

//给定数组,统计各个数字的最大和。

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

 

best-time-to-buy-and-sell-stock-ii