Middle-题目19:309. Best Time to Buy and Sell Stock with Cooldown

原创 2016年05月31日 15:42:52

题目原文:
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)
题目大意:
Middle-题目4相同,区别是卖出股票之后会有一天的CD(冷却期),而在CD的时候是不能买入的。求最大利益。
题目分析:
维护两个dp数组buy和sell,其中buy代表第n天时手里有股票的收益,sell代表第n天时手里无股票的收益。
接下来的转移方程不是很好想到:
buy[i] = max(sell[i-2]-price[i],buy[i-1])
第i天时手里有股票,这时候看第i-1天在做什么:
(1) 如果是买入,则第i天要想手里有股票,什么也不能做,此时buy[i]=buy[i-1].
(2) 如果是卖出,则第i天是CD,啥也干不了,手里一定是没有股票的,这种情况无解。
(3) 如果什么也没做,则第i-2天把股票卖了,手里没股票,并在第i天买入。此时buy[i]=sell[i-2]+price[i]
Sell[i] = max(buy[i-1]+price[i],sell[i-1])
故buy[i]是两者的最大值。
第i天时手里没有股票,这时候还是看第i-1天在做什么:
(1) 如果是买入,则第i-1天是买入,第i天卖掉,此时sell[i]=buy[i-1]+price[i]
(2) 如果是卖出,则第i天是cd,此时sell[i]=sell[i-1]
(3) 如果啥也没做,而手里有股票,还是sell[i]=sell[i-1]
同理sell[i]也是两者最大值。
最后的最大利益是sell[i]的值。因为到最后必须把股票卖了。
因为只跟前两项有关系,所以也是维护两项即可。
源码:(language:java)

public class Solution {
    public int maxProfit(int[] prices) {
        int buy=-999999,preBuy=0,sell=0,preSell=0;
        for(int price : prices) {
            preBuy = buy;
            buy = Math.max(preSell - price, preBuy);
            preSell = sell;
            sell = Math.max(preBuy + price, preSell);
        }
        return sell;       
    }
}

成绩:
1ms,beats 87.89%,众数2ms,40.16%
Cmershen的碎碎念:
DP问题的关键是找出转移数组和转移方程。而这道题的转移方程对我来说比较难想,应多注意。

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

leetcode_middle_46_309. Best Time to Buy and Sell Stock with Cooldown

题意; 一个数组表示一支股票的每一天的价格,每一天可以买入或者卖出,但是买入时不能已经拥有这只股票,且卖出和买入必须间隔一天。 分析:

309.Best Time to Buy and Sell Stock with Cooldown

1.基本思路:此题可通过DP来解决,但状态转移方程的构想不是那么明显,首先可以通过两个数组来表示买与卖的信息: buy[i]:表示第i天为止,最近一次股票购入后所持有的利润 sell[i]:表示第i天...

LeetCode笔记:309. Best Time to Buy and Sell Stock with Cooldown

使用动态规划计算股票买入、卖出、休息三种操作后的最大收益

Leetcode 309. 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. ...

[Leetcode] 309. 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. ...

309. Best Time to Buy and Sell Stock with Cooldown -Medium

Question Say you have an array for which the ith element is the price of a given stock on day i. ...

Leetcode 309. Best Time to Buy and Sell Stock with Cooldown

这道题初开始有一些不知所措,所以参考了一下网上的状态转移方程。题目Say you have an array for which the ith element is the price of a g...

309. Best Time to Buy and Sell Stock with Cooldown

参考: (1)点击打开链接 class Solution { public: int maxProfit(vector& prices) { int len = price...

【LeetCode】309. 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 a...

309. Best Time to Buy and Sell Stock with Cooldown

题目:Best Time to Buy and Sell Stock with Cooldown原题链接:https://leetcode.com/problems/best-time-to-buy-...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)