[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 ...
随机推荐
- 设置windows2008系统缓存大小限制,解决服务器运行久了因物理内存耗尽出僵死(提升权限后,使用SetSystemFileCacheSize API函数,并将此做成了一个Service)
声明: 找到服务器僵死的原因了,原因是虚拟内存设置小于物理内存. 只要虚拟内存设置为系统默认大小就不会出生僵死的现象了. 当时因为服务器内存48G,系统默认虚拟内存大小也是48G, 觉得太占硬盘空间, ...
- Qt - 设置程序界面风格(现成的QMacStyle等等)
类的继承关系: QMotifStyle:OSF(开放基金协会)开发的一个工业标准的GUI(图形用户接口): QCDEStyle:公共桌面环境(Common Desktop Environment)的缩 ...
- 这里有123个黑客必备的Python工具!
123个Python渗透测试工具,当然不仅于渗透~ 如果你想参与漏洞研究.逆向工程和渗透,我建议你时候用Python语言.Python已经有很多完善可用的库,我将在这里把他们列出来. 这个清单里的工具 ...
- python列表的内建函数
list.append(obj) 向列表中添加一个对象obj list.count(obj) 返回一个对象obj 在列表中出现的次数 list.extend(seq)a 把序列seq 的内容添加到列表 ...
- 推荐一个高效,易用功能强大的可视化API管理平台
项目管理 提供基本的项目分组,项目管理,接口管理功能 接口管理 友好的接口文档,基于websocket的多人协作接口编辑功能和类postman测试工具,让多人协作成倍提升开发效率 MockServer ...
- ASP.NET MVC/Core表单提交后台模型二级属性验证问题
起因 这个是网友在官网论坛的提问:https://fineui.com/bbs/forum.php?mod=viewthread&tid=22237 重新问题 本着务实求真的态度,我们先来复现 ...
- SkyWorking基础:6.2版本安装部署
就在今天,SkyWorking发布了6.2版本. 概述 什么是SkyWorking SkyWalking是观察性分析平台和应用性能管理系统. 提供分布式追踪.服务网格遥测分析.度量聚合和可视化一体化解 ...
- 关于vue中使用rem问题
1.下载lib-flexible 我使用的是vue-cli+webpack,所以是通过npm来安装的 npm i lib-flexible --save 2.引入lib-flexible 在main. ...
- 【hadoop+spark】搭建spark过程
部分转载,已标红源地址,本博客为本菜搭建与爬坑记录,整理版请看: https://blog.csdn.net/the_fool_/article/details/78211166 记录: ====== ...
- 私服 Nexus 的配置 (转)
https://www.cnblogs.com/yuanermen/p/4574473.html 一.概述 1.概要 现在的项目基本都是用Maven来管理工程,这样一来在公司内容搭建一个私服就非常有必 ...