【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 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]
提示:
这道题可以用动态规划的思路解决。但是一开始想的时候总是抽象不出状态转移方程来,之后看到了一种用状态机的思路,觉得很清晰,特此拿来分享,先看如下状态转移图:

这里我们把状态分成了三个,根据每个状态的指向,我们可以得出下面的状态转移方程:
- s0[i] = max(s0[i-1], s2[i-1])
- s1[i] = max(s1[i-1], s0[i-1] - price[i])
- s2[i] = s1[i-1] + price[i]
这样就清晰了很多。
代码:
class Solution {
public:
int maxProfit(vector<int>& prices){
if (prices.size() <= ) return ;
vector<int> s0(prices.size(), );
vector<int> s1(prices.size(), );
vector<int> s2(prices.size(), );
s1[] = -prices[];
s0[] = ;
s2[] = INT_MIN;
for (int i = ; i < prices.size(); i++) {
s0[i] = max(s0[i - ], s2[i - ]);
s1[i] = max(s1[i - ], s0[i - ] - prices[i]);
s2[i] = s1[i - ] + prices[i];
}
return max(s0[prices.size() - ], s2[prices.size() - ]);
}
};
【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown的更多相关文章
- 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【leetcode】123. Best Time to Buy and Sell Stock III
@requires_authorization @author johnsondu @create_time 2015.7.22 19:04 @url [Best Time to Buy and Se ...
- 【LeetCode】188. Best Time to Buy and Sell Stock IV 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】714. Best Time to Buy and Sell Stock with Transaction Fee
题目如下: Your are given an array of integers prices, for which the i-th element is the price of a given ...
- 【LeetCode】121. Best Time to Buy and Sell Stock 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 C++ 解法 日期 ...
- 【LeetCode】123. Best Time to Buy and Sell Stock III 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】122.Best Time to Buy and Sell Stock II 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】714. Best Time to Buy and Sell Stock with Transaction Fee 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【LeetCode】122. Best Time to Buy and Sell Stock II
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
随机推荐
- WinRAR5.01注册码附注册机
把下面的注册码复制到"记事本"中,另存为"rarreg.key"文件,放到WinRAR安装目录即完成注册.RAR registration datakjcy8U ...
- 深度解析MySQL启动时报“The server quit without updating PID file”错误的原因
很多童鞋在启动mysql的时候,碰到过这个错误, 首先,澄清一点,出现这个错误的前提是:通过服务脚本来启动mysql.通过mysqld_safe或mysqld启动mysql实例并不会报这个错误. 那么 ...
- 消息:SQL Server 2017(vNext)的第三个公开的CTP(社区技术预览版)发布了
今天看到了一个新闻,跟大家分享一下,有兴趣的可以去尝试一下. SQL Server 2017 CTP3于5月23日发布了,详细版本号是6.7.55.0. 大家可以去安装试试.在下载页面,目前是SQL ...
- MySQL高可用方案MHA自动Failover与手动Failover的实践及原理
集群信息 角色 IP地址 ServerID 类型 Master ...
- [原创]LAMP+phpmyadmin+FTP环境搭建
***简单ftp服务器搭建: rpm –qa|grep vsftpd //检查是否安装服务 yum –y install vsftpd-* //安装服务 mkdir /var/ftp/uplo ...
- 什么样的PPT能助你成为一个优秀的演讲者——程序员的演讲之道
欢迎访问我的个人博客,原文链接:http://wensibo.top/2017/05/28/speaker/ ,未经允许不得转载! 前言 今天是端午节假期的第一天,在这里祝大家假期快乐,不过像我这种渣 ...
- 【2017-05-30】WebForm文件上传
用 FileUpload控件进行上传文件. <asp:FileUpload ID="FileUpload1" runat="server" /> ...
- Python可视化:Seaborn库热力图使用进阶
前言 在日常工作中,经常可以见到各种各种精美的热力图,热力图的应用非常广泛,下面一起来学习下Python的Seaborn库中热力图(heatmap)如何来进行使用. 本次运行的环境为: windows ...
- HTML----网页基础和基本标签
网页分类: 1.静态网页:所有内容全写死,都写在源代码中,若修改必须修改源代码,后缀为.html或htm 2.动态网页:内容大部分来自于数据库,可以修改,后缀为.aspx(c#).jsp(java). ...
- TCP慢启动,拥塞控制,ECN 笔记
TCP慢启动,拥塞控制,ECN 笔记 1,TCP慢启动 TCP在连接过程的三次握手完成后,开始传数据,并不是一开始向网络通道中发送大量的数据包,这样很容易导致网络中路由器缓存空间耗尽,从而发生拥塞:而 ...