解题报告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:
Input: [1,2,3,0,2]
Output: 3
Explanation: transactions = [buy, sell, cooldown, buy, sell]
解题思路:
第一种方案, 假设数组长度为n, dp[i][j]为从i到j所能达到的最大收益,那么本题即求dp[0][n - 1],
对于dp[i][j], 其可能的cooldown位置有 I, i + 1, ..., j - 1, j,
所以存在递推关系
dp[i][j] = max{ dp[i][k - 1] + dp[k + 1][j]} k = i, i + 1, ... , j - 1, j
当k == i 时, dp[i][k - 1] 不存在,即只有dp[k + 1][j], 同理
当k == j 时, dp[k + 1][j] 不存在,即只有dp[i][k - 1]
prices[j] - prices[I] 为dp[I][j]的初始值
所以最终dp[i][j] = max(prices[j] - prices[I], max{dp[i][k - 1] + dp[k + 1][j]})
而题目希望求解的是dp[0][n - 1]. 所以i 从n-1往0求解,j从0往n-1求解
时间复杂度O(n^3) 空间复杂度O(n^2)
代码如下
class Solution {
public:
//suppose cooldown at k
//dp[i][j] = max{dp[i][k - 1] + dp[k + 1][j]} k = i ... j
int maxProfit(vector<int>& prices) {
int n = prices.size();
if (0 == n) return 0;
vector<vector<int>> dp(n, vector<int>(n, 0));
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
dp[i][j] = prices[j] - prices[i];
}
}
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < n; j++) {
//cout<<"i="<<i<<" j="<<j<<" "<<dp[i][j]<<endl;
for (int k = i; k < j; k++) {
int tmp = 0;
if (k - 1 >= i) {
tmp += dp[i][k - 1];
}
if (k + 1 <= j) {
tmp += dp[k + 1][j];
}
dp[i][j] = max(dp[i][j], tmp);
}
}
}
return dp[0][n - 1];
}
};
第二种方案:顺序DP
常规的DP的类型主要有三类,矩阵dp,一个一维数组的dp,两个一维数组的dp
矩阵dp 构造f[i][j], 一维dp构造f[i], 两个一维dp构造f[i][j]
本题恰好可以使用顺序dp,而且是一维的数组
解题思路:
每一天股票的持有状态可能有三种情况
cool down-->buy-->sell-->cool down-->buy-->sell-->cool down
状态转换的关系如上, leetcode讨论区有人画了状态图,非常容易理解, 参考链接
https://leetcode.com/explore/interview/card/top-interview-questions-hard/121/dynamic-programming/862/discuss/75928/Share-my-DP-solution-(By-State-Machine-Thinking)
也就是说
buy的状态 可能是从前一个buy 或者前一个cool down过来
sell的状态 只能是从前一个buy过来
cool down的状态 可能是从前一个cool down或者前一个sell的状态过来
这里需要搞清楚
1)sell 和 cool down的区别, sell状态只有 卖出的那个时刻状态是保持的, 卖完第二天状态就是cool down了.
2)buy 到 sell 之间的这段时间,按题意并不算cool down,而全是buy状态
3)sell 到 cool down之间的这段时间,全是cool down状态
由此可以得出
buy[i] = max(buy[i - 1], rest[i - 1] - prices[I]) // 这里用rest 表示 cool down
rest[i] = max(rest[i - 1], sell[I - 1])
sell[I] = buy[I - 1] + prices[i]
代码如下
java
class Solution {
public int maxProfit(int[] prices) {
int n = prices.length;
if (0 == n) return 0;
int[] buy = new int[n];
int[] rest = new int[n];
int[] sell = new int[n];
buy[0] = -prices[0];
rest[0] = 0;
sell[0] = Integer.MIN_VALUE;
for (int i = 1; i < n; i++) {
buy[i] = Math.max(buy[i - 1], rest[i - 1] - prices[i]);
rest[i] = Math.max(rest[i - 1], sell[i - 1]);
sell[i] = buy[i - 1] + prices[i];
}
return Math.max(rest[n - 1], sell[n - 1]);
}
}
c++
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
if (0 == n) return 0;
vector<int> buy(n, 0);
vector<int> rest(n, 0);
vector<int> sell(n, 0);
buy[0] = -prices[0];
rest[0] = 0;
//不可能存在,所以收益取最小,因为i位置,我们希望取的是最大值,
//将sell设置为最小值,表示永远不可能取该值
sell[0] = INT_MIN;
for (int i = 1; i < n; i++) {
buy[i] = max(buy[i - 1], rest[i - 1] - prices[i]);
rest[i] = max(rest[i - 1], sell[i - 1]);
sell[i] = buy[i - 1] + prices[i];
}
return max(rest[n - 1], sell[n - 1]);
}
};
解题报告Best Time to Buy and Sell Stock with Cooldown的更多相关文章
- LeetCode解题报告—— Best Time to Buy and Sell Stock
Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...
- leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown
121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- Leetcode之动态规划(DP)专题-309. 最佳买卖股票时机含冷冻期(Best Time to Buy and Sell Stock with Cooldown)
Leetcode之动态规划(DP)专题-309. 最佳买卖股票时机含冷冻期(Best Time to Buy and Sell Stock with Cooldown) 股票问题: 121. 买卖股票 ...
- 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- [LeetCode] 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 al ...
- [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 an al ...
- 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 al ...
- LeetCode Best Time to Buy and Sell Stock with Cooldown
原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ 题目: Say you hav ...
- 121. 122. 123. 188. Best Time to Buy and Sell Stock *HARD* 309. Best Time to Buy and Sell Stock with Cooldown -- 买卖股票
121. Say you have an array for which the ith element is the price of a given stock on day i. If you ...
随机推荐
- MEF学习总结(4)---Container层
通过AttributeedModelPrograming,我们可以声明暴露组件,依赖组件,发现组件,但是所有这些需要有一个触发点.即需要把所有这些组合在一起来协同工作,最终实现依赖注入.这就是Cont ...
- Python nltk English Detection
http://blog.alejandronolla.com/2013/05/15/detecting-text-language-with-python-and-nltk/ >>> ...
- Go的List操作上的一个小“坑”
转自http://sharecore.net/blog/2014/01/09/the-trap-in-golang-list/ 一直想不清楚一个问题,简单设计的东西到底是“坑多”还是“坑少”呢? 复杂 ...
- send函数和recv函数
1.send 函数 int send( SOCKET s, const char FAR *buf, int len, int flags ); 不论是客户还是服务器应用程序都用send函数来向T ...
- GOF23设计模式之策略模式(strategy)
一.策略模式概述 策略模式对应于解决某一个问题的一个算法族,允许用户从该算法族中任选一种算法解决一个问题,同时可以方便的更换算法或者增加新的算法.并且由客户端决定调用哪个算法. 策略模式的本质: 分离 ...
- Java实现HTML转换为PDF的常见方法
最近在自己的项目中需要动态生成融资单合同,这里需要把对应的html转换为对应的pdf融资合同.因此需要通过Java实现将HTML转PDF.自己之前没有接触过这一块的东西,所以上网查了一下,网上有很多的 ...
- Linux下压缩与解压
转自:http://www.mike.org.cn/blog/index.php?load=read&id=218###pp=0 [在解压或压缩的时候,一般还使用-v选项来现实正在处理的文件信 ...
- Converter(转换器)与Formatter(格式化) ,Validator(验证器)
Converter(转换器)与Formatter(格式化)都可以用于将一种对象类型转换为另一种对象类型.Converter是通用元件,可以在应用程序的任意层中使用,而Fotermatter这是专门为W ...
- 关于alter database open resetlogs及incarnation的一点理解
关于alter database open resetlogs及incarnation的一点理解 不完全恢复只能做一次吗? 采用rman的默认设置,对数据库进行了backup database备份,进 ...
- Spring AOP详解及简单应用
Spring AOP详解 一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址: ...