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 = [1, 2, 3, 0, 2]
maxProfit = 3
transactions = [buy, sell, cooldown, buy, sell]
这道题目自己一开始根本没有想到用动态规划,菜鸟本色。之后看了dicussion区域排名第一的答案,也是云里雾里。
还好碰到了这个解决思路。这个方案把每天分为四个状态
  1. 有股票,卖 = 前天(有股票,休息)+ price 或者 前天(没股票,买)+ price
  2. 有股票,休息 = 前天(有股票,休息)或者 前天(没股票,买)
  3. 没股票,买 = 前天(没股票,休息)- price ==》 冷却期所以不可能是 有股票,卖
  4. 没股票,休息 = 前天(没股票,休息)或者 前天(有股票,卖)

结合这个思路,编码实现如下:

需要额外注意的一点是第0天的初始化,(有股票,休息)= -price 因为下一天可能卖这个股票,所以计价时相当于第0天买了。

    int maxProfit(vector<int>& prices) {

        int has_sell, has_sell_before;
int has_rest, has_rest_before;
int no_buy, no_buy_before;
int no_rest, no_rest_before; int size = prices.size();
if(size < )
return ;
has_sell_before = ;
has_rest_before = -prices[];
no_buy_before = -prices[];
no_rest_before = ;
for(int i = ; i < size; i++)
{
has_sell = max(has_rest_before + prices[i], no_buy_before + prices[i]);
has_rest = max(has_rest_before, no_buy_before);
no_buy = no_rest_before - prices[i];
no_rest = max(no_rest_before, has_sell_before); has_sell_before = has_sell;
has_rest_before = has_rest;
no_buy_before = no_buy;
no_rest_before = no_rest;
} // find the max between has_sell and no_rest
return max(has_sell, no_rest);
}

leetcode笔记(一)309. Best Time to Buy and Sell Stock with Cooldown的更多相关文章

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

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

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

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

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

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

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

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

  9. [leetcode] 309. Best Time to Buy and Sell Stock with Cooldown(medium)

    原题 思路: 状态转移 出售股票的状态,最大利润有两种可能. 一,和昨天一样不动:二,昨天持有的股票今天卖掉. sell[i] = max(sell[i-1],buy[i-1] + prices[i] ...

随机推荐

  1. gulp前端自动化构建工具

    博主不易,不求赞赏,希望把自己遇到的难点写出来,以及希望自己能有能力写出一篇不错的博文. 前端构建工具本人 bootstrap+jquery用gulp vue+element 用webpack 引文 ...

  2. Python爬取天气预报

    实现爬取一天的天气预报 非常简单的一个小爬虫,利用的也是基本的request.BeautifulSoup.re库,算是简单的上手一个小测试吧 from urllib.request import ur ...

  3. 项目 08 WebSocket

    项目班 08 WebSocket app.py 更新 添加两个路由 handlers = [ ('/', main.IndexHandler), ('/explore', main.ExploreHa ...

  4. HDU 5877 Weak Pair DFS + 树状数组 + 其实不用离散化

    http://acm.hdu.edu.cn/listproblem.php?vol=49 给定一颗树,然后对于每一个节点,找到它的任何一个祖先u,如果num[u] * num[v] <= k.则 ...

  5. webservice初识,SOAP1.1版本

    客户端与服务端模式,非web端发布 1.1      [Jax-ws第一个例子] 1.1.1     第一步:服务端开发 编写SEI(Service Endpoint Interface),SEI在w ...

  6. int,long,long long的数据范围

    unsigned   int   0-4294967295   int   2147483648-2147483647 unsigned long 0-4294967295long   2147483 ...

  7. ngnix入门配置

    文件1.首先到ngnix下载页面下载你操作系统对应的ngnix压缩包    http://nginx.org/en/download.html 博主我是window10操作系统  上面是我解压之后放的 ...

  8. vue-elem-配置静态模拟数据访问接口

    使用本地mock数据模拟真实数据配置 static/data.json dev.server.js中 var app=express();之后添加以下代码, var appData=require(' ...

  9. 用xaml画的带阴影3D感的圆球

    <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center"> <E ...

  10. Python开发环境Wing IDE搜索工具介绍

    Wing IDE编辑器的搜索工具提供了一个基于友好GUI的搜索和替换工具. 某些情况下搜索可能会跨越整个文件,也有可能被限制到当前所选择的区域:可以区分大小写,也可以设置为不区分:可以被限制为只匹配整 ...