题目链接:https://leetcode.com/problems/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]

思路:一道有点诡异的动态规划.因为存在冷却时间

1.在第i天买一支股票还能剩下的利润=第(i-2)天销售能够剩余的利润-第i天股票的价钱.

2.在第i天卖一支股票总的利润=第(i-1)天买股票剩下的最大利润+当前股票的价格.

也就是说需要维护两个状态的信息,一个是买股票所得到的剩余最大利润,一个是卖出股票之后得到的最大利润,他们互相依赖对方的信息.

再来进一步分析如何维持一个最大的利润.

对于买来说,当天是否买取决于买了之后是否比之前买所剩余的利润大,即状态转移方程为:

buy[i] = max(buy[i-1], sell[i-2] - prices[i-1]);

对于卖来说,同样当天是否将这只股票卖掉取决于卖掉能否获得更大的利润,状态转移方程为:

sell[i] = max(sell[i-1], buy[i-1] + prices[i-1]);

代码如下:

class Solution {
public:int maxProfit(vector<int>& prices) {if(prices.size() ==0) return 0;int len = prices.size();vector<int> buy(len+1, 0), sell(len+1, 0);buy[1] = -prices[0];for(int i = 2; i <= len; i++){buy[i] = max(sell[i-2]- prices[i-1], buy[i-1]);sell[i] = max(sell[i-1], buy[i-1]+prices[i-1]);}return sell[len];}
};

[leetcode] 309. Best Time to Buy and Sell Stock with Cooldown 解题报告相关推荐

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

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

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

  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. 【Java力扣算法】LeetCode 309 Best Time to Buy and Sell Stock with Cooldown(动态规划)

    题目: 给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 .​ 设计一个算法计算出最大利润.在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票): 你不能同时参与多笔交 ...

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

    这道题用的是dp的思想,用buy, sell, cooldown记录当前这些状态下最多的profit. 对于day i 而言: sell[i] = buy[i-1] + prices[i] buy[i ...

  6. leetcode 309. Best Time to Buy and Sell Stock with Cooldown | 309. 最佳买卖股票时机含冷冻期(动态规划)

    题目 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ 题解 我知道要dp,但是不知道要怎么dp ...

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

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

  9. LeetCode 309. Best Time to Buy and Sell Stock with Cooldown--Java解法-卖股票系列题目

    此文首发于我的Jekyll博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 题目地址: ...

最新文章

  1. GOF23种设计模式(Design Pattern)总结
  2. Oracle PL/SQL编程之过程
  3. RabbitMQ声明队列参数作用详解
  4. Laravel报错Failed opening required ‘bootstrap/../vendor/autoload.php‘
  5. 『ACM--数据结构--字典树』信息竞赛进阶指南--Tire树
  6. java实现邮件发送准备工作(前期配置)
  7. cdr 表格自动填充文字_做平面广告设计,AI和CDR如何选择?
  8. Gensee移动SDK之(一)结构组成
  9. 02.C(数据类型与运算符)
  10. Windows Server 2016-Wbadmin命令行备份域控制器
  11. 湖南文理学院计算机宿舍,湖南文理学院宿舍怎么样 住宿条件好不好
  12. VirtualBox虚拟机中安装XP系统
  13. ipv4和计算机地址是什么意思,什么是IP地址?IPV4和IPV6又有什么区别?
  14. android 获取视频码率和缓存大小,android - 使用MediaCodec和MediaMuxer录制视频,但比特率和帧率不正确 - 堆栈内存溢出...
  15. 北京休闲好去处 适合春游的地方
  16. 三丁基-巯基膦烷「tBuBrettPhos Pd(allyl)」OTf),1798782-17-8
  17. qt.network.monitor: Could not get the INetworkConnection instance for the adapter GUID.QT关闭时程序异常结束
  18. 什么是MySQL| 什么是数据库 | 数据库详解
  19. 英文中 vi和vt的区别
  20. BeatBox终极版——Head first java 650

热门文章

  1. 华为服务器磁盘没显示不出来,服务器磁盘读取不了
  2. 美团数据分析岗面试题+解题思路
  3. 【金融大屏项目】—— Echarts水滴图(echarts-liquidfill)
  4. 《人类简史》笔记二——一场永远的革命
  5. YOLOV5训练代码train.py注释与解析
  6. 「股价飙到100美元我就纹身」,黄仁勋用十年站在了芯片塔尖
  7. 联想服务器ghost系统进不去系统,联想电脑台式机开机后一直在这个界面进不了系统是为什么。...
  8. OJ笔记 18939 最长单词
  9. 像个黑客一样在网络上来无影去无踪之IP代理理论篇
  10. 计算机云计算论文范文,云计算环境下计算机管理系统论文