Best Time to Buy and Sell Stock with Cooldown -- LeetCode
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 = [, , , , ]
maxProfit =
transactions = [buy, sell, cooldown, buy, sell]
思路:DP。
因为这个题中有两种操作buy 和 sell,这里我们用buy[i]表示在第i天或之前买入股票所能获得的最大利益(即最后一次交易为买入股票),用sell[i]表示在第i天或之前卖出股票所能获得的最大利益。我们分别考虑转化公式。
对于buy[i],我们有两种选择,一是这一天不买入股票,则最大利润为buy[i-1];或者这一天买入股票,因为买入股票的前一天不能卖出股票,则最大利润应该是两天之前卖出股票的最大利润减去今日的股价sell[i-2] - prices[i]。所以
buy[i] = max(buy[i-], sell[i-] - prices[i])
对于sell[i],我们也有两种选择,一是这一天不卖出股票,则最大利润为sell[i-1];或者这一天卖出股票,则为前一天为止买入股票所能获得的最大利润加上今日的股价buy[i-1] + prices[i]。所以
sell[i] = max(sell[i-], buy[i-] + prices[i])
完整程序:
class Solution {
public:
int maxProfit(vector<int>& prices) {
if (prices.size() == ) return ;
vector<int> buy(prices.size());
vector<int> sell(prices.size());
sell[] = ;
buy[] = -prices[];
for (int i = ; i < prices.size(); i++) {
buy[i] = std::max(buy[i-], (i > ? sell[i-] : ) - prices[i]);
sell[i] = std::max(sell[i-], buy[i-] + prices[i]);
}
return sell.back();
}
};
实际上,我们可以做到O(1)空间复杂度。
class Solution {
public:
int maxProfit(vector<int>& prices) {
if (prices.size() == ) return ;
int prev_sell(), prev_buy, sell(), buy(INT_MIN);
for (int i = , n = prices.size(); i < n; i++) {
prev_buy = buy;
buy = std::max(prev_buy, prev_sell - prices[i]);
prev_sell = sell;
sell = std::max(prev_sell, prev_buy + prices[i]);
}
return sell;
}
};
Best Time to Buy and Sell Stock with Cooldown -- LeetCode的更多相关文章
- 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之动态规划(DP)专题-309. 最佳买卖股票时机含冷冻期(Best Time to Buy and Sell Stock with Cooldown)
Leetcode之动态规划(DP)专题-309. 最佳买卖股票时机含冷冻期(Best Time to Buy and Sell Stock with Cooldown) 股票问题: 121. 买卖股票 ...
- [LeetCode] 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 买卖股票的最佳时间有冷却期
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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 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 Best Time to Buy and Sell Stock with Cooldown
原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ 题目: Say you hav ...
- 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 ...
随机推荐
- python 3 使用cmp函数报错
python3 中已经不使用cmp函数进行比较大小,使用operator模块 import operator lt(a,b) 相当于 a<b 从第一个数字或字母(ASCII)比大小 le(a,b ...
- paramiko类Fabric主机管理
环境:Linux python3.5 要求:类 Fabric 主机管理程序开发:1. 运行程序列出主机组或者主机列表2. 选择指定主机或主机组3. 选择让主机或者主机组执行命令或者向其传输文件(上传/ ...
- 安装启动Apache2.4后报Invalid command 'Order', perhaps misspelled or defined by a module not included in the server configuration错误
LoadModule access_compat_module modules/mod_access_compat.so 取消这一行模块的注释,再重启服务即可. 搜索 mod_access_compa ...
- 用Linkedhashmap的LRU特性及SoftReference软引用构建二级缓存
LRU: least recently used(近期最少使用算法).LinkedHashMap构造函数可以指定其迭代顺序:LinkedHashMap(int initialCapacity, flo ...
- RMAN 增量备份级别说明
通过Bat批处理调用RMan是我们定时备份数据库的好帮手,但是RMan的备份级别需要我们好好了解一下. RMAN备份全为全备和增量备份 增量备份:分为0 1 2级 ORACLE官方解释: A leve ...
- HTML5初识Canvas
HTML5初识Canvas <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...
- 【linux】如何解决VMWare上linux虚拟机连不上外网的问题?
>>>故障现象:虚拟机连接不到外网? >>>故障背景: Centos7.4发行版本: 虚拟机和VM软件都是nat模式: 注意这里默认的VMWare的DHCP服务时开 ...
- [洛谷P4889]kls与flag
题目大意:有$n$根竹竿,第$i$根竹竿在$i$位置,第$i$根竹竿高度为$h_i$,每根竹竿可以向左倒或向右倒,问有几对竹竿倒下后顶端重合. 题解:求出每根竹竿倒下后的位置,离散化,记录一下每个 ...
- BZOJ1025 [SCOI2009]游戏 【置换群 + 背包dp】
题目链接 BZOJ1025 题解 题意就是问一个\(1....n\)的排列在同一个置换不断重复下回到\(1...n\)可能需要的次数的个数 和置换群也没太大关系 我们只需知道同一个置换不断重复,实际上 ...
- 【POJ 2752 Seek the Name, Seek the Fame】
Time Limit: 2000MSMemory Limit: 65536K Description The little cat is so famous, that many couples tr ...