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. 小小粉丝度度熊 二分答案 + two pointer

    http://acm.hdu.edu.cn/showproblem.php?pid=6119 发现自己的two pointer能力超弱. 这题是合并时间后,二分答案. 可以知道对于每个时间区间,合法的 ...

  2. 将本地代码添加到github

    首先在github上创建一个仓库. 第一步:建立本地仓库 git init 关联远程仓库 git remote add origin https://github.com/tshua/***.git ...

  3. 使用pycharm 运行python的django项目时报错“Quit the server with CTRL-BREAK.”

    Quit the server with CTRL-BREAK.Error: [Errno 10013] 1昨晚测试时还好好的,怎么突然出现这个错误,于是GOOLE,找到个帖子说可能是端口占用了,用工 ...

  4. hadoop集群搭建简要记录

    2019/03/09 21:46 准备4台服务器或者虚拟机[centos7],分别设置好静态ip[之所以设置静态ip主要就是为了省心!!!][ centos7下面配置静态IP  参考地址: https ...

  5. nodejs入门学习笔记一——一个完整的http路由服务实现

    开始学习nodejs! 参考书籍:The Node Beginner Book ,所有问题和讨论都围绕本书. 1.学习nodejs需要具备的基础知识: js基本语法,基本上写过前端的都能满足,原生js ...

  6. Django组件:用户认证组件

    一丶用户认证 1.auth模块 from django.contrib import auth django.contrib.auth中提供了许多方法,这里主要介绍其中的三个: (1).authent ...

  7. JavaScript 创建对象的七种方式

    转自:xxxgitone.github.io/2017/06/10/JavaScript创建对象的七种方式/ JavaScript创建对象的方式有很多,通过Object构造函数或对象字面量的方式也可以 ...

  8. mui的ajax例子1

    mui.ajax()方法,get请求 前端页面: <!DOCTYPE html><html><head> <meta charset="utf-8& ...

  9. 使用HTML5 canvas做地图(3)图片加载平移放大缩小

    终于开始可以写代码了,手都开始痒了.这里的代码仅仅是在chrome检测过,我可以肯定的是IE10以下浏览器是行不通,我一直在考虑,是不是使用IE禁止看我的篇博客,就是这群使用IE的人,给我加了很多工作 ...

  10. 如何使用VS将项目生成一个安装包?

    VS2010项目的部署与安装winform程序,我想进行安装.1.在解决方案中 ——点击右键——添加 2.然后选择 安装和部署 ——安装向导 可以更改名称 3.点击 下一步 4.然后选择上那3个 5. ...