代码随想录训练营day51, 买卖股票最佳时机含冷冻期, 买卖股票最佳时机含手续费

买卖股票最佳时机含冷冻期

卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天)

数组定义: dp[i][j],第i天状态为j,所剩的最多现金为dp[i][j]

四种状态分析:

  1. 买入/持有股票状态 0
  2. 卖出股票
    1. 两天前就卖出了股票,度过了冷冻期,一直没操作 1
    2. 今天卖出了股票 2
  3. 今天为冷冻期 3

递推公式:

  1. 达到状态1: dp[i][0]

    操作一:

    前一天就是持有股票状态: dp[i - 1][0]

    操作二: 今天买入了

    a. 前一天是冷冻期: dp[i - 1][3] - prices[i]

    b. 前一天是保持卖出股票状态: dp[i - 1][1] - prices[i]

    (然后先给操作二取最大值, 然后max全部)

  2. 达到状态2: dp[i][1]

    操作一:

    前一天就是状态二: dp[i - 1][1]

    操作二:

    前一天就是冷冻期: dp[i - 1][3]

  3. 达到状态3: dp[i][2]

    昨天一定是买入股票状态: dp[i - 1][0] + prices[i]

  4. 达到状态4: dp[i][3]

    昨天一定是卖出了股票: dp[i - 1][2]

初始化:

0, 1, 2, 3四个状态, 只有0是买入, 所以给他 = -prices[0]就行

最后return的时候, 也要去状态二三四的最大值: 卖出两种先比

看代码: 做出来的决定因素就是能够理解四个状态和递推方程

class Solution {
    public int maxProfit(int[] prices) {
        int len = prices.length;
        if(len == 0 || prices == null){
            return 0;
        }
        //create the dp
        int[][] dp = new int[len + 1][4];
        //inni, only 0 is the state of buy
        dp[0][0] = -prices[0];
        for(int i = 1; i < len; i++){
            //0--buy/have stocks 1--sold stock no cold  2--sell stock today 3--cold
            //dp[i][0]means have stocks--buy today-(cold before/normal before)/already have         
            dp[i][0] = Math.max(Math.max(dp[i - 1][3] - prices[i], dp[i - 1][1] - prices[i]), dp[i - 1][0]);
            //1-be able to sell, but haven't --- cold before/same as before
            dp[i][1] = Math.max(dp[i - 1][3], dp[i - 1][1]);
            //2-sell today means: sell today get cash and yesterday have stock
            dp[i][2] = dp[i - 1][0] + prices[i];
            //3-cold--yesterday must be sell
            dp[i][3] = dp[i - 1][2];
        }
        return Math.max(Math.max(dp[len - 1][1], dp[len - 1][2]), dp[len - 1][3]);
    }
}

一维数组优化版, 把日子后移一天

class Solution {
    public int maxProfit(int[] prices) {
        int len = prices.length;
        if(len == 0 || prices == null){
            return 0;
        }
        //create the dp
        int[] dp = new int[4];
        //inni, only 0 is the state of buy
        dp[0] = -prices[0];
        //这里变成<=len, 因为下面没有i维了
        for(int i = 1; i <= len; i++){
            //接下俩的买入和卖出会不断变化, 所以要用temp保存
            int temp0= dp[0];
            int temp2 = dp[2];
            //还记得之前转换一维怎么做的吗, 把日子后移一天, 所以price[i-1]
            dp[0] = Math.max(Math.max(dp[3] - prices[i - 1], dp[1] - prices[i - 1]), dp[0]);
            dp[1] = Math.max(dp[3], dp[1]);
            dp[2] = temp0 + prices[i - 1];
            dp[3] = temp2;
        }
        return Math.max(Math.max(dp[1], dp[2]), dp[3]);
    }
}

买卖股票最佳时机含手续费

笔交易都要交等于fee的手续费

贪心算法

(把fee包含在股票价格里, 然后需要定义一个反悔操作)

https://www.notion.so/Best-Time-to-Buy-and-Sell-Stock-with-Transaction-Fee-3cc7598311614334b0cb4ef355a8e8c1

动态规划

和买卖股票II的唯一区别, 就是减去手续费就行了

不持有股票的情况: MAX(之前就卖了, 今天卖的-fee)

class Solution {
    public int maxProfit(int[] prices, int fee) {
        int len = prices.length;
        if(len == 0 || prices == null){
            return 0;
        }
        int[][] dp = new int[len + 1][2];
        //浅浅初始化一波
        dp[0][0] = -prices[0];
        for(int i = 1; i < len; i++){
            //buy today/ bought before
            dp[i][0] = Math.max(dp[i - 1][1] - prices[i], dp[i - 1][0]);
            //sell today/ sought before
            dp[i][1] = Math.max(dp[i - 1][0] + prices[i] - fee, dp[i - 1][1]);
        }
        return dp[len - 1][1];
    }
}

同样可以一维

class Solution {
    public int maxProfit(int[] prices, int fee) {
        int len = prices.length;
        if(len == 0 || prices == null){
            return 0;
        }
        int[] dp = new int[2];
        //浅浅初始化一波
        dp[0] = -prices[0];
        for(int i = 1; i <= len; i++){
            //buy today/ bought before
            dp[0] = Math.max(dp[1] - prices[i - 1], dp[0]);
            //sell today/ sought before
            dp[1] = Math.max(dp[0] + prices[i - 1] - fee, dp[1]);
        }
        return dp[1];
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值