题目

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 ... jint 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]);}
};

转载于:https://www.cnblogs.com/ctrlzhang/p/9536256.html

解题报告Best Time to Buy and Sell Stock with Cooldown相关推荐

  1. 【LeetCode】解题309:Best Time to Buy and Sell Stock with Cooldown(动态规划)

    LeetCode解题 309:Best Time to Buy and Sell Stock with Cooldown (动态规划) Problem 309: Best Time to Buy an ...

  2. 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 最佳买卖股票时机含冷冻期(Medium)(JAVA)

    [LeetCode]309. Best Time to Buy and Sell Stock with Cooldown 最佳买卖股票时机含冷冻期(Medium)(JAVA) 题目地址: https: ...

  3. 【DP + 卖股票】LeetCode 309. Best Time to Buy and Sell Stock with Cooldown

    LeetCode 309. Best Time to Buy and Sell Stock with Cooldown Solution1: 比较有难度的一道动态规划题了! 参考网址:http://z ...

  4. [刷题]leetcode #309 - Best Time to Buy and Sell Stock with Cooldown

    题目 代码 class Solution {public int maxProfit(int[] prices) {if (prices.length <= 1) return 0;int l ...

  5. 309. Best Time to Buy and Sell Stock with Cooldown**

    309. Best Time to Buy and Sell Stock with Cooldown** https://leetcode.com/problems/best-time-to-buy- ...

  6. 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 解题报告(Python C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  7. 【leetcode-Python】-Dynamic Programming -309. Best Time to Buy and Sell Stock with Cooldown

    题目链接 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ 题目描述 给定价格数组prices, ...

  8. [leetcode] 309. Best Time to Buy and Sell Stock with Cooldown 解题报告

    题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ Say you have an ar ...

  9. 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 ...

最新文章

  1. ubuntu操作系统下载
  2. 腾讯音乐招 iOS 开发, base 深圳,要求:本科、三年、OC,懂音视频开发优先。...
  3. 递推DP HDOJ 5459 Jesus Is Here
  4. 岭回归和lasso回归_正则化(2):与岭回归相似的 Lasso 回归
  5. 操作系统 第二章【进程管理】【MOOC答案】
  6. 字典生成工具_CANOpen系列教程09_CANOpen对象字典
  7. 【LightOJ - 1079】Just another Robbery(概率dp,概率背包)
  8. 周鸿祎:希望将互联网基因与汽车制造企业基因进行重组
  9. 蛇形填数 ------- 模拟水题
  10. JavaScript的事件绑定及深入
  11. 软件开发报价的计算方法
  12. mysql gay锁_MySQL事务(二) - osc_gay6i4ve的个人空间 - OSCHINA - 中文开源技术交流社区...
  13. [b][size=5]沸点文库下载器V2.5.0.0——百度文库豆丁道客巴巴实时浏览下载热门排行文档推荐[/size][/b] [b]沸点文库下载器 - 沸点文档下载 软件信息 软件版本:V2.5.
  14. ps4插html屏幕不亮光,ps4连接显示器怎么老是黑屏
  15. 52个数据可视化图表鉴赏
  16. 读取XML文件时,总报“前言中不允许有内容”
  17. animate cc mysql,Vue 动画渲染
  18. 360全景拼接 opencv_360度视频序列全景图拼接系统的设计与实现
  19. nginx 三级域名泛解析并指向某文件 带参数
  20. Java基础(二)集合

热门文章

  1. Polyspace应用:Bug Finder检测代码漏洞
  2. Python笔记之自定义函数
  3. 720 词典中最长的单词(Trie树)
  4. 图格 Pro for mac(图片拼图切图大师)
  5. processing实现图像碎片化
  6. linux 使用了哪个存储阵列卡,Linux中RAID概述及配置实验
  7. 笔记本linux蓝牙驱动怎么安装程序,如何安装蓝牙设备的驱动程序
  8. xp电脑自动锁定计算机,WinXP系统如何设置电脑自动关机?
  9. 020.验证二叉搜索树
  10. 人耳能听的声音范围与各种发音的频率范围