• leetcode每日一题(2020-07-10):309. 最佳买卖股票时机含冷冻期


    由于该题是套题中的第五题,所以我全都列出来,描述就只写本题的了
    1.只允许交易一次:121. 买卖股票的最佳时机
    2.无交易次数上限:122. 买卖股票的最佳时机 II
    3.只允许交易两次123.买卖股票的最佳时机 III
    4.有交易次数上限188. 买卖股票的最佳时机 IV
    5.每次交易后休息:309. 最佳买卖股票时机含冷冻期
    6.每次交易要费用:714. 买卖股票的最佳时机含手续费
    题目描述:
    给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 。​
    设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):

    你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
    卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天)。
    

    今日学习:
    1.动规动规动动规

    题解:
    首先参考:模板动规:一穿六
    状态转移图:

    状态转移方程:

    base case:
    dp[-1][k][0] = dp[i][0][0] = 0
    dp[-1][k][1] = dp[i][0][1] = -infinity
    状态转移方程:
    dp[i][k][0] = max(dp[i-1][k][0], dp[i-1][k][1] + prices[i])
    dp[i][k][1] = max(dp[i-1][k][1], dp[i-1][k-1][0] - prices[i])
    
    k为1或不限制时,都可以省略k的存在,当k有限制时需要循环k进行枚举。
    

    题解1只允许交易一次:

    /**
     * @param {number[]} prices
     * @return {number}
     */
    //暴力法
    var maxProfit = function(prices) {
        let res = 0
        for(let i = 0; i < prices.length - 1; i++) {
            let max = Math.max(...prices.slice(i + 1))
            res = Math.max(res, max - prices[i])
        }
        return res
    };
    //动规
    var maxProfit = function(prices) {
        let min = Number.MAX_VALUE
        let res = 0
        for(let i = 0; i < prices.length; i++) {
            if(prices[i] < min) {
                min = prices[i]
            }
            res = Math.max(res, prices[i] - min)
        }
        return res
    }
    //模板动规
    var maxProfit = function(prices) {
        let n = prices.length
        let dp_i_0 = 0, dp_i_1 = -prices[0]
        for(let i = 0; i < n; i++) {
            dp_i_0 = Math.max(dp_i_0, dp_i_1 + prices[i])
            dp_i_1 = Math.max(dp_i_1, -prices[i])
        }
        return dp_i_0
    }
    

    题解2无交易次数上限:

    /**
     * @param {number[]} prices
     * @return {number}
     */
    //峰谷
    var maxProfit = function(prices) {
        let i = 0
        let valley = peak = prices[0]
        let max = 0
        let len = prices.length
        while(i < len - 1) {
            while(i < len - 1 && prices[i] >= prices[i + 1]) {
                i++
            }
            valley = prices[i]
            while(i < len - 1 && prices[i] <= prices[i + 1]) {
                i++
            }
            peak = prices[i]
            max += peak - valley
        }
        return max
    };
    //连续计算
    var maxProfit = function(prices) {
        let max = 0
        for(let i = 1; i < prices.length; i++) {
            if(prices[i] > prices[i - 1]) {
                max += prices[i] - prices[i - 1]
            }
        }
        return max
    }
    //模板动规
    var maxProfit = function(prices) {
        let n = prices.length
        let dp_i_0 = 0, dp_i_1 = -prices[0]
        for(let i = 0; i < n; i++) {
            dp_i_0 = Math.max(dp_i_0, dp_i_1 + prices[i])
            dp_i_1 = Math.max(dp_i_1, dp_i_0 - prices[i])
        }
        return dp_i_0
    }
    

    题解3只允许交易两次:

    /**
     * @param {number[]} prices
     * @return {number}
     */
    //暴力法
    var maxProfit = function(prices) {
        let max = 0
        for(let i = 0; i < prices.length; i++) {
            let p1 = prices.slice(0, i + 1)
            let p2 = prices.slice(i + 1)
            max = Math.max(max, maxProfitOnce(p1) + maxProfitOnce(p2))
        }
        return max
    };
    var maxProfitOnce = function(prices) {
        let min = Number.MAX_VALUE
        let res = 0
        for(let i = 0; i < prices.length; i++) {
            if(prices[i] < min) {
                min = prices[i]
            }
            res = Math.max(res, prices[i] - min)
        }
        return res
    }
    //动规
    var maxProfit = function(prices) {
        let n = prices.length;
        if(n == 0){
            return 0;
        }
        let maxTime = 2;
        let dp = Array.from(new Array(n),() => new Array(maxTime+1));
        for(let i = 0;i < n;i++){
            for(let r = 0;r <= maxTime;r++){
                dp[i][r] = new Array(2).fill(0);
            }
        }
        for(let i = 0;i < n;i++){
            for(let k = maxTime;k >= 1;k--){
                if(i == 0){
                    dp[i][k][0] = 0;
                    dp[i][k][1] = -prices[i];
                    continue;
                }
                dp[i][k][0] = Math.max(dp[i-1][k][0],dp[i-1][k][1] + prices[i]);
                dp[i][k][1] = Math.max(dp[i-1][k][1],dp[i-1][k-1][0] - prices[i]);
            }
        }
        return dp[n-1][maxTime][0];
    };
    //模板动规
    var maxProfit = function(prices) {
        let n = prices.length
        //第一次交易:没持有(没买)利润是0,持有(买了)利润是-prices[0]
        let dp_i_1_0 = 0, dp_i_1_1 = -prices[0]
        //初始第二次交易和第一次相同
        let dp_i_2_0 = 0, dp_i_2_1 = -prices[0]
        for(let i = 1; i < n; i++) {
            dp_i_1_0 = Math.max(dp_i_1_0, dp_i_1_1 + prices[i])
            dp_i_1_1 = Math.max(dp_i_1_1, -prices[i])
            dp_i_2_0 = Math.max(dp_i_2_0, dp_i_2_1 + prices[i])
            dp_i_2_1 = Math.max(dp_i_2_1, dp_i_1_0 - prices[i])
        }
        return dp_i_2_0
    };
    

    题解4有交易次数上限:

    /**
     * @param {number} k
     * @param {number[]} prices
     * @return {number}
     */
    var maxProfit = function(k, prices) {
        let n = prices.length
        if (k > n / 2)  {return maxProfitINF(prices)}
        let dp = Array.from(
            {length: n+1},() => Array.from(
                {length: k + 1},() => new Array(2))
            )
        for(let K = 0; K <= k; K++) {
            dp[0][K][0] = 0;
            dp[0][K][1] = -prices[0];
        }
        for(let i = 1; i < n + 1; i++) {
            dp[i][0][0] = 0;
            dp[i][0][1] = -prices[0];
            for(let j = 1; j < k + 1; j++) {
                dp[i][j][0] = Math.max(dp[i-1][j][0],dp[i-1][j][1] + prices[i-1]);
                dp[i][j][1] = Math.max(dp[i-1][j][1],dp[i-1][j-1][0]-prices[i-1]);
            }
        }
        return dp[n][k][0];
    };
    var maxProfitINF = function(prices) {
        let n = prices.length
        let dp_i_0 = 0, dp_i_1 = -prices[0]
        for(let i = 0; i < n; i++) {
            dp_i_0 = Math.max(dp_i_0, dp_i_1 + prices[i])
            dp_i_1 = Math.max(dp_i_1, dp_i_0 - prices[i])
        }
        return dp_i_0
    }
    

    题解5每次交易后休息:

    /**
     * @param {number[]} prices
     * @return {number}
     */
    var maxProfit = function(prices) {
        let n = prices.length
        let dp_i_0 = 0, dp_i_1 = -prices[0]
        let dp_pre_0 = 0
        for(let i = 0; i < n; i++) {
            let temp = dp_i_0
            dp_i_0 = Math.max(dp_i_0, dp_i_1 + prices[i])
            dp_i_1 = Math.max(dp_i_1, dp_pre_0 - prices[i])
            dp_pre_0 = temp
        }
        return dp_i_0
    };
    

    题解6每次交易要费用:

    /**
     * @param {number[]} prices
     * @param {number} fee
     * @return {number}
     */
    var maxProfit = function(prices, fee) {
        let n = prices.length
        let dp_i_0 = 0, dp_i_1 = -prices[0]
        for(let i = 0; i < n; i++) {
            dp_i_0 = Math.max(dp_i_0, dp_i_1 + prices[i] - fee)
            dp_i_1 = Math.max(dp_i_1, dp_i_0 - prices[i])
        }
        return dp_i_0
    };
    
  • 相关阅读:
    linux下的make命令
    安装pytorch
    CondaError: Downloaded bytes did not match Content-Length
    cv2.VideoCapture(0)
    cv2.imread(),cv2.imshow(),cv2.imwrite()
    unsqueeze()和squeeze()
    django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module
    Can't connect to MySQL server on '127.0.0.1' (10061)
    ping ip
    _vimrc配置
  • 原文地址:https://www.cnblogs.com/autumn-starrysky/p/13278672.html
Copyright © 2020-2023  润新知