int maxProfit(vector<int> &prices) {
     int len = prices.size();
     if(!len) return 0;
     vector<int> profitLeft(len, 0);
     vector<int> profitRight(len, 0);
     int maxp = 0, low = prices[0], high = prices[len-1];
     for(int i=1; i<len; ++i) {
         if(prices[i] < low) low = prices[i];
         profitLeft[i] = max(profitLeft[i-1], prices[i]-low);
     }
     for(int i=len-2; i>=0; --i) {
         if(prices[i] > high) high = prices[i];
         profitRight[i] = max(profitRight[i+1], high-prices[i]);
         maxp = max(maxp, profitLeft[i]+profitRight[i]);
     }
     return maxp;
 }
Ejemplo n.º 2
0
    int maxProfit(vector<int>& prices) {
        int n = prices.size();
        if(n < 2) return 0;
        vector<int> minValLeft(n, 0);
        vector<int> maxValLeft(n, 0);
        vector<int> minValRight(n, 0);
        vector<int> maxValRight(n, 0);
        vector<int> profitLeft(n, 0);
        vector<int> profitRight(n, 0);
        minValLeft[0] = prices[0];
        maxValLeft[0] = prices[0];
        minValRight[n - 1] = prices[n - 1];
        maxValRight[n - 1] = prices[n - 1];
        profitLeft[0] = 0;
        profitRight[n - 1] = 0;

        for(int i = 1; i < n; i++) {
            minValLeft[i] = min(minValLeft[i - 1], prices[i]);
            maxValLeft[i] = max(maxValLeft[i - 1], prices[i]);
            profitLeft[i] = max(profitLeft[i - 1], prices[i] - minValLeft[i - 1]);
        }

        for(int i = n - 2; i >= 0; i--) {
            minValRight[i] = min(minValRight[i + 1], prices[i]);
            maxValRight[i] = max(maxValRight[i + 1], prices[i]);
            profitRight[i] = max(profitRight[i + 1], maxValRight[i + 1] - prices[i]);
        }
        
        int res = 0;
        for(int i = 0; i < n - 1; i++) {
            res = max(res, maxValRight[i + 1] - minValLeft[i]);
        }
        
        for(int i = 0; i < n - 1; i++) {
            res = max(res, profitLeft[i] + profitRight[i + 1]);
        }

        return res;
    }