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全栈工程师(运算符、if)
ParisGabriel Python 入门基础 比较运算符:< 小于<= 小于等于> 大于>= 大于等于== 等于!= 不等于 语法: 表达式1>表达式 ...
- Tensorflow实现LSTM识别MINIST
import tensorflow as tf import numpy as np from tensorflow.contrib import rnn from tensorflow.exampl ...
- 软工实践 - 第十一次作业 Alpha 冲刺 (3/10)
队名:起床一起肝活队 组长博客:https://www.cnblogs.com/dawnduck/p/9972061.html 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去 ...
- intellij idea 2017 工具使用问题
1.打开idea 打开maven项目报错:Unable to import maven project 2.在idea中Help->Show Log in Explorer->idea.l ...
- CSLM 配置粗解
CSLM工具(continuous space language model toolkit)用于训练NNLM,支持SRILM.KENLM(默认)语言模型工具,CUDA加速,CSTM统计机器翻译. 本 ...
- [xsy1129] flow [树链剖分和线段树一起优化网络流][我也不知道这是什么鬼标签]
题面 内部OJ 思路 考虑一个决策方案${x}$,$x_i$表示第$i$个点选不选,$f^k_i$表示点$i$的第$k$个父亲 那么可以得到总花费的表达式$ans=\sum V_i x_i - \su ...
- TJOI2018游记
D1T1 - 数学计算 直接用线段树/平衡树维护所有数的积即可.我思想僵化写了一个数学方法...应该是能做\(\bmod\)所有数的乘除法. 时间复杂度\(O(nlogn)\). D1T2 - 智力竞 ...
- 《R语言实战》读书笔记--第四章 基本数据管理
本章内容: 操纵日期和缺失值 熟悉数据类型的转换 变量的创建和重编码 数据集的排序,合并与取子集 选入和丢弃变量 多说一句,数据预处理的时间是最长的……确实是这样的,额. 4.1一个示例 4.2创建新 ...
- Java并发(1)- 聊聊Java内存模型
引言 在计算机系统的发展过程中,由于CPU的运算速度和计算机存储速度之间巨大的差距.为了解决CPU的运算速度和计算机存储速度之间巨大的差距,设计人员在CPU和计算机存储之间加入了高速缓存来做为他们之间 ...
- 转:Java SoftReference 使用构建对象缓存
本文介绍对象的强.软.弱和虚引用的概念.应用及其在UML中的表示. 1.对象的强.软.弱和虚引用 在JDK 1.2以前的版本中,若一个对象不被任何变量引用,那么程序就无法再使用这个对象.也就是说, ...