LeetCode 309 Best Time to Buy and Sell Stock with Cooldown 解决方案
题目描述
给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 。
设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):
- 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
- 卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天)。
示例:
输入: [1,2,3,0,2]
输出: 3
解释: 对应的交易状态为: [买入, 卖出, 冷冻期, 买入, 卖出]
解决方案1
可以通过一个状态转换图,来解决。
定义状态
s0 : 没有买股票时
s1 :买入股票且没有卖出股票时
s2:卖出股票是
那么状态转换可以这样表示
so->s1 买股票 s0->s0不操作
s1->s2 卖股票 s1->s1不操作
s2->s0 经过冷冻期 s2->s2 不操作
转换图也可以画出来:
那么每一次价格变动都会在这三个状态里
得出递推式
s0[i] = max(s0[i - 1], s2[i - 1]); 不操作 和 冷冻期过去
s1[i] = max(s1[i - 1], s0[i - 1] - prices[i]); 不操作 和 买股票
s2[i] = s1[i - 1] + prices[i]; 卖出股票
最终的状态应该会在s0和s2终态度有关, 因为s1是卖出股票 是 会比s0小的,
最优解表示为 max(s0[最后一次价格变动],s2[最后一次价格变动])
具体代码如下;
class Solution {
public:
int maxProfit(vector<int>& prices){
if (prices.size() <= 1) return 0;
vector<int> s0(prices.size(), 0);
vector<int> s1(prices.size(), 0);
vector<int> s2(prices.size(), 0);
//初始化
s1[0] = -prices[0];
s0[0] = 0;
s2[0] = INT_MIN;
//状态转移
for (int i = 1; i < prices.size(); i++) {
s0[i] = max(s0[i - 1], s2[i - 1]);
s1[i] = max(s1[i - 1], s0[i - 1] - prices[i]);
s2[i] = s1[i - 1] + prices[i];
}
return max(s0[prices.size() - 1], s2[prices.size() - 1]);
}
};
总结
这个动态规划问题竟然能用一种状态转换的方式来得到也是很神奇了, 动态规划关键是找到此时最优解和上一次的最优解之间的关系,而这个题目最优解是有2个状态产生的,而状态直接又有着紧密的关系,所以用状态转换图来理清关系,找到最优解的更新表示,最终得到结果。还是值得深思的。
LeetCode 309 Best Time to Buy and Sell Stock with Cooldown 解决方案的更多相关文章
- [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 ...
- LeetCode 309. Best Time to Buy and Sell Stock with Cooldown (stock problem)
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 ...
- [leetcode] 309. Best Time to Buy and Sell Stock with Cooldown(medium)
原题 思路: 状态转移 出售股票的状态,最大利润有两种可能. 一,和昨天一样不动:二,昨天持有的股票今天卖掉. sell[i] = max(sell[i-1],buy[i-1] + prices[i] ...
- 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】309. Best Time to Buy and Sell Stock with Cooldown 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 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 ...
- 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 ...
- 【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 ...
随机推荐
- Python Ethical Hacking - Malware Packaging(2)
PACKAGING FOR WINDOWS FROM LINUX For best results package the program from the same OS as the target ...
- Porter 进入 CNCF 云原生全景图,新版本即将发布!
近日,KubeSphere 社区子项目面向物理机环境的负载均衡器 Porter 正式进入 CNCF Landscape.CNCF Landscape 在云原生实践过程中的每个环节帮助用户了解有哪些具体 ...
- Python 编程语言的核心是什么?
01 Python 编程语言的核心是什么? 为什么要问这个问题? 我想要用Python实现WebAssembly,这并不是什么秘密.这不仅可以让Python进入浏览器,而且由于iOS和Andr ...
- JavaSE基础知识之修饰符和使用场景,你真的了解嘛
修饰符的作用是啥? 用来定义类.方法或者变量的访问权限 两大类 访问修饰符 限定类.属性或方法是否可以被程序里的其他部分访问和调用的修饰符 private<default<protecte ...
- Java 并发实践 — ConcurrentHashMap 与 CAS
转载 http://www.importnew.com/26035.html 最近在做接口限流时涉及到了一个有意思问题,牵扯出了关于concurrentHashMap的一些用法,以及CAS的一些概念. ...
- element上传功能携带参数
在写element的上传功能时,需要对上传的文件携带参数,但是参数比较多,就需要一个对象合并的方法,Object.assign() Object.assign(target, source1, sou ...
- Windows下给PHP安装redis扩展
一.选择适合的版本 二.下载扩展 官网下载地址:http://pecl.php.net/package/redis ,选择合适的版本进行下载 三.解压后将下面两个文件复制到PHP扩展文件目录(ext文 ...
- 360随身WiFi3:纯净版无线网卡驱动下载及安装教程(Windows10版本)
对于不带无线网卡的台式机,买一个360随身WiFi当无线网卡是很省钱的方法.但是这个随身WiFi3用的芯片较为奇葩,Win10下不太好找驱动.什么,你问我为啥不用360官网上的驱动?那个“驱动”装了之 ...
- 国内安装Homebrew
原文链接更详细 命令 $ /bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew. ...
- ElasticJob和SpringBoot
本文以在SpringBoot下集成ElasticJob的方式对其进行浅析,仅仅是简单使用,不涉及源码级别研究. 事先必备: 注册中心——zookeeper 简略结构: 代码目录结构: ├─.idea ...