Best Time to Buy and Sell Stock with Cooldown

 

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 as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:

  • You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
  • After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)

Example:

prices = [1, 2, 3, 0, 2]
maxProfit = 3
transactions = [buy, sell, cooldown, buy, sell]

思路:因为当前日期买卖股票会受到之前日期买卖股票行为的影响,首先考虑到用DP解决。

 

这道题比较麻烦的是有个cooldown的限制,其实本质也就是买与卖之间的限制。对于某一天,股票有三种状态: buy, sell, cooldown, sell与cooldown我们可以合并成一种状态,因为手里最终都没股票,最终需要的结果是sell,即手里股票卖了获得最大利润。所以我们可以用两个DP数组分别记录当前持股跟未持股的状态。然后根据题目中的限制条件,理清两个DP数组的表达式。

对于当天最终未持股的状态,最终最大利润有两种可能,一是今天没动作跟昨天未持股状态一样,二是昨天持股了,今天卖了。所以我们只要取这两者之间最大值即可,表达式如下:


sellDp[i] = Math.max(sellDp[i - 1], buyDp[i - 1] + prices[i]);

对于当天最终持股的状态,最终最大利润有两种可能,一是今天没动作跟昨天持股状态一样,二是前天还没持股,今天买了股票,这里是因为cooldown的原因,所以今天买股要追溯到前天的状态。我们只要取这两者之间最大值即可,表达式如下:


buyDp[i] = Math.max(buyDp[i - 1], sellDp[i - 2] - prices[i]);

最终我们要求的结果是


sellDp[n - 1] 表示最后一天结束时手里没股票时的累积最大利润
public class Solution {
    public int maxProfit(int[] prices) {
        if(prices == null || prices.length == 0) return 0;
        int[] buyDp = new int[prices.length];
        int[] sellDp = new int[prices.length];
        
        buyDp[0] = -prices[0];
        sellDp[0] = 0;
        for(int i=1; i<prices.length; i++){
            sellDp[i] = Math.max(sellDp[i-1], buyDp[i-1]+prices[i]);
            if(i>=2){
                 buyDp[i] = Math.max(buyDp[i-1], sellDp[i-2]-prices[i]);
            } else {
                buyDp[i] = Math.max(buyDp[i-1], -prices[i]);
            }
        }
        return sellDp[prices.length-1];
    }
}

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值