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 ...
随机推荐
- jmeter运行脚本后,请求偶发性的传参错误
问题现象:jmeter写好脚本后,请求偶发性的传参错误 排查过程:1.结合报错返回值,看是不是线程并发引起: 2.排除线程并发引起后,看看是不是取值策略:如果是参数化,看看是不是每次迭代,每次都取唯一 ...
- python基础实践(一)
-*-纸上得来终觉浅,绝知此事要恭行.-*-# -*- coding:utf-8 -*-# Author:sweeping-monkname = "什么是字符串?"What_is_ ...
- Python全栈工程师(数值类型、运算符)
ParisGabriel Python 入门基础 python的应用领域: 1.系统运维 2.网络编程(如:网络爬虫,搜索引擎,服务器编程) 3.科学计算 4.航空领域(如:卫星, ...
- XPS Enable GPIO on EMIO interface 不见了
按照 <嵌入式系统软硬件协同设计实战指南 -- 基于xilinx Zynq>第九章 zedboard 入门 我一步一步做到9.1.2 (13) 发现 没有 Enable GPOI on ...
- qemu中device和driver的区别 使用9p文件系统
qemu配置中经常会出现-driver/-device的选项,可以理解成-driver是后端设备,即一个实际的物理的磁盘:device是把这块磁盘插入到虚机中的pci控制器中. 这样的话,虚机也能看到 ...
- 【bzoj2424】[HAOI2010]订货 费用流
原文地址:http://www.cnblogs.com/GXZlegend/p/6825296.html 题目描述 某公司估计市场在第i个月对某产品的需求量为Ui,已知在第i月该产品的订货单价为di, ...
- Using Let’s Encrypt for free SSL Certs with Netscaler
Using Let’s Encrypt for free SSL Certs with Netscaler If you haven’t heard, Let’s Encrypt (https://l ...
- Batting Practice LightOJ - 1408
Batting Practice LightOJ - 1408(概率dp) 题意:有无限个球,进球的概率为p,问你连续不进k1个球或者连续进k2个球需要使用的球的个数的期望 思路: \(定义f[i]表 ...
- codeforces school mark(贪心)
///太渣,看了题解才知道怎么做,自己想感觉想不清楚 ///题解:首先在给出的序列里判断小于median的个数,若大于(n-1)/2,则不满足,否则看另一个条件 ///这样我们可以把中位数左边还要添加 ...
- JavaScript的团队编程规范
本规范是针对javascript函数式编程风格与公司严重依赖于jQuery进行编码的现实制定出来. 禁止使用eval,with与caller(ecma262 v5 的use strict要求).eva ...