[leetcode] 309. Best Time to Buy and Sell Stock with Cooldown(medium)
原题
思路:
状态转移
出售股票的状态,最大利润有两种可能。
一,和昨天一样不动;二,昨天持有的股票今天卖掉。
sell[i] = max(sell[i-1],buy[i-1] + prices[i]);
购买股票的状态,最大利润有两种可能。
一,和昨天一样不动;二,两天前出售,今天购买。
bdp[i] = Math.max(bdp[i-1],sell[i-2] - prices[i]);
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(buy[i - 1], sell[i - 2] - prices[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(medium)的更多相关文章
- [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 解决方案
题目描述 给定一个整数数组,其中第 i 个元素代表了第 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 ...
随机推荐
- 多态与虚拟 : 物件导向的精髓 (侯捷在石器时代对OO的理解)
[自序]虑而後能得(自序)故事接触 C++ 大约是 1989 年的事.那时候的 PC 以现在的眼光看,除了「蛮荒」之外没有更合适的形容词了.横扫千军的 Windows 3.0 还没有诞生,如今以 C+ ...
- Qt5 结构及模块组成?
作为一个Qt的粉丝,对将于明年发布的Qt5充满了期待.可是想想Qt5将发生的巨大变化,心底又有点不安.Qt5到底会变成什么样呢? 看看近期Qt5的一些大动作: 从 QtCore中移除 QSetting ...
- PHP发送邮件功能实现(使用163邮箱)
第一步 我用的是163邮箱发送邮件,做一个尝试,在尝试之前,需要要开启163邮箱的授权码如图所示,请记住您的授权码,将在之后的步骤中用到 第二步 需要下载一个类PHPMailer,我有这个资源已经上传 ...
- c# 停靠窗体
public partial class FrmAnchor : Form, IMessageFilter { public FrmAnchor(Control parentControlc, Con ...
- dedecms自学
[dedecms笔记] index.htm 主页模板head.htm 列表头部模板footer.htm ...
- openstack namespace 的应用
查看虚拟机网络连通性 1.neutron port-list | grep IP 2.neutron port-show ID 查看subnet 3.neutron subnet-show ID 查看 ...
- centos 5.5版本中添加ext4格式
1.我在使用centos 5.5版本做练习的时候发现默认是不支持ext4文件格式. 在添加硬盘后,用fdisk -l 查看到信息如下: 分区完后,使用命令:mkfs -t ext4 /dev/sdb会 ...
- tomcat配置多个数据源
应用场景: 公司tomcat服务器中运行着多个工程,工程链接的mysql数据库不同,所以每个工程的Spring总配置文件中都配置了数据源. 需求: 将数据源统一拿到tomcat中配置. 本来指派给 ...
- jieba GitHUb 结巴分词
1.GitHub jieba-analysis 结巴分词: https://github.com/fxsjy/jieba 2.jieba-analysis 结巴分词(java版): https://g ...
- docker-compose exec时 出现"fork/exec /proc/self/exe: no such file or directory" 报错
问题:跟往常一样执行docker-compos exec redis sh时出现如下错误,而容器是运行状态中. # docker-compose exec redis sh rpc error: co ...