题目要求

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]

和前面几题相比,这题还增加了一个限制条件,也就是说我们在抛出股票之后,还需要冷却一天才可以买入下一只股票。那么我们进行什么样的操作才能使收益最大呢?

思路和代码

这里转述leetcode上一个非常漂亮的解答。
在第i天时,我们可以进行两种操作,抛出或是买入或是啥都不干。但是具体下来,又有四种情况:

  1. 在持有一只股票的时候抛出
  2. 在持有一只股票的时候啥都不干
  3. 在持有0只股票的时候啥都不干
  4. 在持有0只股票的时候买入

而在这些操作之间又存在潜在的联系,也就是说我如果在第i天进行以上四种操作之一,那么意味着我在第i-1天一定进行了这四种操作中的某一种,从而支持我第i天的操作。具体关联如下:

  1. 第i天执行的操作:在持有一只股票的时候抛出 => 在第i-1天执行的操作: 在持有一只股票的时候啥都不干/在持有0只股票的时候买入
  2. 第i天执行的操作:在持有一只股票的时候啥也不干 => 在第i-1天执行的操作:在持有一只股票的时候啥也不干/在持有0只股票的会后买入
  3. 第i天执行的操作:在持有0只股票的时候买入 => 在第i-1天执行的操作:在持有0只股票的时候啥也不做
  4. 第i天执行的操作:在持有0只股票的时候啥也不做 => 在第i-1天执行的操作:在持有0只股票的时候啥也不做/在持有一只股票的时候抛出

我们采用动态规划的思想,分别记录第i-1天的时候这四种情况的最大收入,并由此比较并得出第i天时这四种情况的最大收入。最后比较最后一天这四种情况可以得到的最大收益,代码如下:

    public int maxProfit(int[] prices) {if(prices.length == 0) return 0;int hasOneDoNothing = -prices[0];int hasOneSellIt = 0;int hasZeroDoNothing = 0;int hasZeroBuyOne = -prices[0];for(int i = 1 ; i<prices.length ; i++){int tmp1 = hasOneDoNothing;int tmp2 = hasOneSellIt;int tmp3 = hasZeroDoNothing;int tmp4 = hasZeroBuyOne;hasOneDoNothing = tmp1 > tmp4 ? tmp1 : tmp4;hasOneSellIt = (tmp1 > tmp4 ? tmp1 : tmp4) + prices[i];hasZeroDoNothing = tmp2 > tmp3 ? tmp2 : tmp3;hasZeroBuyOne = tmp3 - prices[i];}return hasZeroDoNothing > hasOneSellIt ? hasZeroDoNothing : hasOneSellIt;}

这里你可能会困惑,为什么只比较 在最后一天持有0只股票并且不进行任何操作在最后一天持有股票并抛出这两种情况呢?
假设我们最后一天持有股票并且不抛出,那么意味着在之前买入最后一只股票的那一天,如果我们不购入将会得到更大的收益。因此抛出一定比不抛出得到的损失小。
至于另一种情况,即最后一天又买入了股票,显然它一定比不买入股票得到的收益少啊。
因此我们只要比较最初提出的两种情况即可。


想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~

Leetcode309. Best time to sell stock with cooldown相关推荐

  1. 【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 ...

  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. [刷题]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 ...

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

  5. 【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 ...

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

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

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

  8. 【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, ...

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

最新文章

  1. 第十五届全国大学生智能车人工智能创意组复赛首批团队名单
  2. 计算机桌面黑屏时间,电脑自动黑屏时间怎么调?
  3. python从文件中读取数据_使用Python脚本从文件读取数据代码实例
  4. java反射的效率有多高
  5. AssertionError: Path does not exist: py-faster-rcnn/data/VOCdevkit2007/VOC2007/ImageSets/Main
  6. 微信小程序 - 使用npm(第三方包)
  7. zabbix简单介绍(1)
  8. linux 思维导图
  9. pycharm修改选中字体颜色
  10. 【Axure报错】-Unable to connect to Axure Share. Please make sure you have an internet connection and try
  11. html5广告拦截器识别代码做提示(本站内容无法显示)
  12. GHOST文件修改OEM的方法
  13. java里什么是索引_面试官:什么是索引?
  14. vue 截取video第一帧作为封面
  15. 《论程序员与老板之间的道德问题》
  16. nginx的 ngx.var ngx.ctx ngx.req
  17. 【机器人学导论(第四版)】1-绪论
  18. 3.6.2数据库系统-范式判断:范式分类、第一范式、第二范式、第三范式、BC范式
  19. Linux安装mysql没有my.cnf文件
  20. 关于HBuilderX的简单介绍

热门文章

  1. DOC命令大全【详细版】
  2. 【Linux】Centos7重启网卡失败
  3. 【Hive】命令行提示符中显示当前所在数据库
  4. 【MySQL】RPM包安装
  5. php调试代码时var_dump( )打印中文字符时出现乱码解决方案
  6. Linux-删除文件空间不释放问题解决
  7. 解决Sql中DIstinct与Order By共同使用的冲突问题
  8. 【虚拟机ubuntu设置ssh】ssh连不上问题解决方法
  9. Elasticsearch分页解决方案
  10. 遇到local variable ‘e‘ referenced before assignment这样的问题应该如何解决