LeetCode Best Time to Buy and Sell Stock III

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        int len = prices.size();
        if (len < 2) return 0;

        vector<int> i2r(len, 0);
        vector<int> i2l(len, 0);

        int max_price = prices[len - 1];

        for (int i=len-2; i>=0; i--) {
            if (prices[i] > max_price) max_price = prices[i];
            int cp = max_price - prices[i];
            i2r[i] = cp > i2r[i + 1] ? cp : i2r[i + 1];
        }

        int min_price = prices[0];

        for (int i=1; i<len; i++) {
            if (prices[i] < min_price) min_price = prices[i];
            int cp = prices[i] - min_price;
            i2l[i] = cp > i2l[i - 1] ? cp : i2l[i - 1];
        }


        int mp = 0;

        for (int i=0; i<len; i++) {
            int p = i2l[i] + i2r[i + 1 >= len ? len - 1 : i + 1];
            if (p > mp) mp = p;
        }

        return mp;
    }
};

参考:http://www.cnblogs.com/zhuli19901106/p/3516813.html

第二轮:

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete at most two transactions.

Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

之差一滴滴就能想到了,可惜:

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        int len = prices.size();
        if (len < 2) {
            return 0;
        }
        vector<int> L(len, 0);
        vector<int> H(len, 0);
        
        L[0] = prices[0];
        H[len-1] = prices[len-1];
        for (int i=1; i<len; i++) {
            L[i] = min(L[i-1], prices[i]);
        }
        
        for (int i=len-2; i>=0; i--) {
            H[i] = max(H[i+1], prices[i]);
        }
        
        vector<int> L2R(len, 0);
        for (int i=1; i<len; i++) {
            L2R[i] = max(L2R[i-1], prices[i] - L[i]);
        }
        
        vector<int> R2L(len, 0);
        for (int i=len-2; i>=0; i--) {
            R2L[i] = max(R2L[i+1], H[i] - prices[i]);
        }
        
        int maxprofit = 0;

        for (int i=0; i<len; i++) {
            maxprofit = max(maxprofit, R2L[i] + L2R[i]);
        }

        return maxprofit;
    }
};
闻说你 时常 在下午 来这里 寄信件
逢礼拜 流连 艺术展 还是未间断
何以我 来回 巡逻遍 仍然和你擦肩
还仍然 在各自宇宙 错过了春天
 
只差一点点 即可以再会面
可惜 偏偏 刚刚 擦过
十面埋伏过 孤单感更赤裸
 
总差一点点 先可以 再会面
仿佛 应该 一早 见过
但直行直过
只差一个眼波 将彼此错过
 
迟两秒 搭上 地下铁 能与你碰上么
如提前 十步 入电梯 谁又被错过
和某某 从来 未预约 为何能见更多
全城来撞你 但最后 处处有险阻
 
只差一点点 即可以再会面
可惜 偏偏 刚刚 擦过
十面埋伏过 孤单感更赤裸
 
总差一点点 先可以 再会面
仿佛 应该 一早 见过
但直行直过 只等一个眼波
轨迹改变 角度交错 寂寞城市又再探戈
天空闪过 灿烂花火 和你不再为爱奔波
 
总差一点点 先可以 再会面
悔不当初 轻轻放过
现在 惩罚我 分手分错了么
分开一千天 天天盼 再会面
只怕是你先找到我 但直行直过天都帮你去躲 躲开不见我
 
discuss上找到个吊炸天的:
public class Solution {
    public int maxProfit(int[] prices) {
        int hold1 = Integer.MIN_VALUE, hold2 = Integer.MIN_VALUE;
        int release1 = 0, release2 = 0;
        for(int i:prices){                              // Assume we only have 0 money at first
            release2 = Math.max(release2, hold2+i);     // The maximum if we've just sold 2nd stock so far.
            hold2    = Math.max(hold2,    release1-i);  // The maximum if we've just buy  2nd stock so far.
            release1 = Math.max(release1, hold1+i);     // The maximum if we've just sold 1nd stock so far.
            hold1    = Math.max(hold1,    -i);          // The maximum if we've just buy  1st stock so far. 
        }
        return release2; ///Since release1 is initiated as 0, so release2 will always higher than release1.
    }
}

 

posted @ 2014-07-22 16:09  卖程序的小歪  阅读(161)  评论(0编辑  收藏  举报