首页 > 代码库 > LeetCode Best Time to Buy and Sell Stock II

LeetCode Best Time to Buy and Sell Stock II

class Solution {public:    int maxProfit(vector<int> &prices) {        int len = prices.size();        if (len < 1) return 0;        int sum = 0;        int last_low = prices[0];        bool increasing = false;        for (int i=1; i<len; i++) {            int cur = prices[i];            if (cur < prices[i - 1]) {                if (increasing) {                    increasing = false;                    sum += prices[i - 1] - last_low;                }                last_low = cur;            } else {                if (increasing) {                                    } else {                    increasing = true;                }            }        }        if (increasing) {            sum += prices[len-1] - last_low;        }        return sum;    }};