(M)Dynamic Programming:309. Best Time to Buy and Sell Stock with Cooldown
此题不会。看大神的分析:
对于这一天是否持股只有两种状态:持股状态(buy),没有持股状态(sell,cooldown)。
对于当天持股状态时,至当天的为止的最大利润有两种可能:1、今天没有买入,跟昨天持股状态一样;2、今天买入,昨天是冷却期,利润是前天卖出股票时候得到的利润减去今天股票的价钱。 二者取最大值。
对于当天未持股状态,至当天为止的最大利润有两种可能:1、今天没有卖出,跟昨天未持股状态一样;2、昨天持有股票,今天卖出了,利润是昨天持有股票时候的利润加上今天股票的价钱。 二者取最大值。
直至最后一天的状态应该是卖出状态。最终利润为sell[n-1];
状态转移方程:
sell[i] = max(sell[i-1], buy[i-1] + price[i]);
buy[i] = max(buy[i-1], sell[i-2] - price[i]);
class Solution {
public:int maxProfit(vector<int>& prices) {int buy(INT_MIN), sell(0), prev_sell(0), prev_buy;for (int price : prices) {prev_buy = buy;buy = max(prev_sell - price, buy);prev_sell = sell;sell = max(prev_buy + price, sell);}return sell;}
};
(M)Dynamic Programming:309. Best Time to Buy and Sell Stock with Cooldown相关推荐
- 【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, ...
- 【leetcode-Python】-Dynamic Programming -122. Best Time to Buy and Sell Stock II
目录 题目链接 题目描述 示例 解题思路 Python实现 时间复杂度与空间复杂度 Python实现·优化空间复杂度 时间复杂度与空间复杂度 解题思路二·贪心算法 Python实现 时间复杂度与空间复 ...
- 【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 ...
- 【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: ...
- 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- ...
- [刷题]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 ...
- 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 解题报告(Python C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 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 ...
- 【Java力扣算法】LeetCode 309 Best Time to Buy and Sell Stock with Cooldown(动态规划)
题目: 给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 . 设计一个算法计算出最大利润.在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票): 你不能同时参与多笔交 ...
最新文章
- Windows2003无法看到网络连接,修复COM+组件
- Java课程03总结
- [one day one question] webpack打包压缩 ES6 js、.vue报错
- 中国经济怎么办之我见
- Windows下C语言连接Oracle数据库
- 知识越分享,收获越多。
- Netty网络编程聊天项目
- SQL Server高级查询之子查询(多行子查询)
- 音频文件压缩大小如何操作?分享一个音频压缩的小技巧
- 穿越火线计算机中丢失,分享一个CF卡数据丢失该如何恢复的方法
- RETINA 屏幕1px 边框实现
- Linux系统如何优化
- thinkphp整合单笔转账到支付宝账户文件配置
- 唯一约束和主键约束的区别
- 【RPO技巧拓展】————3、IIS和.Net应用程序中的非根相对路径覆盖(RPO)
- Step to UEFI (137) 通过 BGRT 取得当前系统的 LOGO
- 非常简单的申请微软学生KEY的方法
- html5备忘录代源码,HTML5备忘录
- c++(小甲鱼笔记01-语言与oo思想)
- 陈志武:教育不转型,国家只能卖苦力