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的更多相关文章

  1. 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

  2. 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. 买卖股票 ...

  3. [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 ...

  4. [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 ...

  5. 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. JavaScript显示当前时间的操作

    JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是在HTML(标 ...

  2. mybatis批量添加、批量删除

    <!-- 批量添加 --> <insert id="insertNameListSynHisBatch" parameterType="java.uti ...

  3. 剖析epool

    [01]什么是epool: 当互联网的用户越来越多的时候,人们发现传统的网络io模型,扛不住用户的高并发请求的时候.各个操作系统给出了自己对应的答案, 而linux给出的答案是epool.epool是 ...

  4. NGUI-为Popuplist的下拉选项添加删除功能

    NGUI例子里的popuplist是这样的:,但有时我们希望下拉选项都有删除功能,也就是这样:,一种方法是改popuplist的源码,我想这个实现起来不难,但现在我想说的是用反射来实现此功能,以及其他 ...

  5. 【Python】重定向 Stream 到文件

    Python 系统模块 sys 中有三个变量 stdin . stdout 与 stderr ,分别对应标准输入流.输出流与错误流.stdin 默认指向键盘, stdout 与 stderr 默认指向 ...

  6. python pip install XXX出现报错问题

    重装Anacondas后,将pip 和python.exe路径加入到环境变量后直接在cmd窗口进行pip 操作,报错如下 报错内容为: pip is configured with locations ...

  7. css深入理解padding

    padding 中规中矩,性格温婉平和! 第一节:CSS padding与容器的尺寸——了解padding与元素尺寸之间关系 CSS padding与容器的尺寸关系复杂 对于block水平元素 没有p ...

  8. node + express + iis + iisnode + urlrewrite搭建站点

    前提条件:安装iis的电脑 准备条件: 1.下载iisnode 地址https://github.com/tjanczuk/iisnode/wiki/iisnode-releases  安装 2.下载 ...

  9. HDU 2491

    欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Priest John's Busiest Day Time Limit: 4000/2000 MS (Java/Others)    ...

  10. J2EE的十三个技术——JSP

    简介 JSP,Java Server  Page,Java服务器页面.它是在传统的网页HTML文件中插入Java程序段(Scriptlet)和JSP标记,从而形成JSP文件,后缀名为(*.jsp). ...