zoukankan      html  css  js  c++  java
  • LeetCode "Best Time to Buy and Sell Stock with Cooldown" !

    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

  • 相关阅读:
    9-单表查询
    02-数据库概述
    01-MySql的前戏
    mysql+centos7+主从复制
    Mac下安装ipython与jupyter
    python开发之virtualenv与virtualenvwrapper讲解
    python操作redis
    权限管理具体代码实现
    docker入门
    多用判断&&
  • 原文地址:https://www.cnblogs.com/tonix/p/4996490.html
Copyright © 2011-2022 走看看