码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode "Best Time to Buy and Sell Stock with Cooldown" !

时间:2015-11-26 06:52:46      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:

Very good DP one.

First I figured out a O(n^2) solution:

class Solution 
{
public:
    int maxProfit(vector<int>& prices) 
    {        
        int n = prices.size();
        if(!n) return 0;
        
        vector<int> dp(n);
        dp[0] = 0;
        for(int i = 1; i < n; i ++)
        {            
            int vn = dp[i-1]; // nothing
            
            // sell
            int vs = 0, sofarMin = INT_MAX;
            for(int j = i - 1; j >= 0; j --)
            {
                sofarMin = min(sofarMin, prices[j]);
                int localProfit = prices[i] - sofarMin;                
                
                int thisChoice = localProfit + (j >= 2? dp[j - 2] : 0);
                vs = max(vs, thisChoice);
            }            
            
            int v = max(vn, vs);
            dp[i] = max(dp[i], v);            
        }
        return dp.back();
    }
};

However by intuition, there must be a O(n) with smarter DP design. Yes!

class Solution 
{
public:
    int maxProfit(vector<int>& prices) 
    {        
        int n = prices.size();
        if(!n) return 0;
        
        vector<vector<int>> dp(n, vector<int>(3, 0)); // 0 buy, 1 rest, 2 sell
        dp[0][0] = -prices[0];        
        for(int i = 1; i < n; i ++)
        {    
            dp[i][0] = max(-prices[i] + dp[i-1][1] /*buy on last rest*/, dp[i-1][0] /* update best buy so far*/); // buy this            
            dp[i][2] = max( prices[i] + dp[i-1][0] /*sell on last buy*/, dp[i-1][2] /* update best sell so far*/); // sell this
            dp[i][1] = dp[i-1][2]; // rest this, somewhat greedy
        }
        
        return dp.back()[2]; // somewhat greedy
    }
};

As you can see, dp[][] vars are rolling. So we don‘t have to use DP array. 3 vars is enough :) - as this link shows:

https://leetcode.com/discuss/71354/share-my-thinking-process

LeetCode "Best Time to Buy and Sell Stock with Cooldown" !

标签:

原文地址:http://www.cnblogs.com/tonix/p/4996490.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!