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:

Input: [1,2,3,0,2]
Output: 3
Explanation: transactions = [buy, sell, cooldown, buy, sell]
----------------------------
 
Solution:

The idea is as follows:

First, think about what we can do on day i? You either have one stock or you don't on day i. For each case, you have two options, making a total of four possible actions on day i:

  1. you have 1 stock and you sell it
  2. you have 1 stock and you do nothing
  3. you have 0 stock and you buy stock i
  4. you have 0 stock and you do nothing

As you can imagine, these four actions are correlated between day i-1 and day i. For example, if you take action 1 on day i, you then have either taken action 2 or 3 on day i-1 but not 1 or 4. In precise, two consecutive days are related as follows:

  1. if you take action 1 on day i ==> you have either taken action 2 or 3 on day i-1
  2. if you take action 2 on day i ==> you have either taken action 2 or 3 on day i-1
  3. if you take action 3 on day i ==> you must have taken action 4 on day i-1 (you can not sell on day i-1 due to cool down)
  4. if you take action 4 on day i ==> you have either taken action 1 or 4 on day i-1

Now you want to maximize your total profit, but you don't know what action to take on day i such that you get the total maximum profit, so you try all 4 actions on every day. Suppose you take action 1 on day i, since there are two possible actions on day i-1, namely actions 2 and 3, you would definitely choose the one that makes your profit on day i more. Same thing for actions 2 and 4. So we now have an iterative algorithm.

Before coding, one detail to emphasize is that the initial value on day 0 is important. You basically cannot take action 1, so the corresponding profits should be 0. You cannot take action 2 in practice, but you cannot set up the profit to 0, because that means you don't have a stock to sell on day 1. Therefore, the initial profit should be negative value of the first stock. You can also think of it as you buy the stock on day -1 and do nothing on day 0.

 public int maxProfit(int[] prices) {

     if (prices.length < 1) return 0;

     int has0_buy = -prices[0];
int has0_doNothing = 0;
int has1_sell = 0;
int has1_doNothing = -prices[0]; for (int i = 1; i < prices.length; i++) {
int l1 = has0_buy;
int l2 = has0_doNothing;
int l3 = has1_sell;
int l4 = has1_doNothing; has0_buy = l2 + -prices[i];
has0_doNothing = Math.max(l3, l2);
has1_sell = Math.max(l1,l4) + prices[i];
has1_doNothing = Math.max(l1, l4);
} return Math.max(has0_doNothing, has1_sell);
}

Leetcode - 309. Best Time to Buy and Sell Stock with Cooldown的更多相关文章

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

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

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

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

  4. LeetCode 309 Best Time to Buy and Sell Stock with Cooldown 解决方案

    题目描述 给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 .​ 设计一个算法计算出最大利润.在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票): 你不能同时参与多笔 ...

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

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

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

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

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

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

随机推荐

  1. shell基础之bash

    一直单单知道部署服务器等命令,shell语言还没有用心学习过,简单的学习下以供不时之需 .sh:bash脚本文件 很多时候需要多个命令来完成一项工作,而这个工作又常常是重复的,这个时候我们自然会想到将 ...

  2. Centos 7最小化安装部署PostgreSQL

    安装 sh-4.2# yum install https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7-x86_64/pgdg-ce ...

  3. java生产环境增量发版陷阱【原】

    前言 在生产环境,我们为了降低发版风险,一般都只做增量发布,不做全量发布. 除非项目只有一到两人开发,对时间线和代码脉络结构一清二楚,才可全量发布. 然而增量发布也是有一定隐藏陷阱在里面的,以下就是笔 ...

  4. MySQL数据库权限体系介绍

    本文主要向大家介绍了MySQL数据库权限体系,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助. 一.权限体系简介: MySQL的权限体系在实现上比较简单,相关权限信息主要存储在mys ...

  5. HDU 1016(素数环 深搜)

    题意是说对一个长度为 n 的数环进行排列,使得相邻两数的和为素数,按从小到大的顺序依次输出. 因为是环,所以总能调整成以 1 为序列首输出.用深度优先搜索的方法即可.在判断素数时由于 n 小于 20, ...

  6. linux_网易云音乐安装

    使用命令安装一些基本包$ sudo apt install devscripts equivs git

  7. vue加载本地json文件

    背景:做地区跟行业级联下拉选择,因为想做成可以搜索的,所以必须一次加载数据,后台有做memcache缓存,但因为数据量大,还是比较费时间,所以做成本地文件,简单记录一下 准备数据,放到static下 ...

  8. Chimee - 简单易用的H5视频播放器解决方案

    Chimee是由奇舞团开源的一套H5视频播放器解决方案,由奇舞团视频云前端团队结合在业务和视频编解码方向的沉淀积累倾心打造.Chimee支持MP4.M3U8.FLV等多种媒体格式,同时它也帮我们解决了 ...

  9. Polymer初探

    Polymer是什么? Polymer英文为 n.聚合物:多聚体 网络高分子:聚合体:高分子聚合物 应用在Web组件场景, 表达的是, 一个一个小的Web组件,可以通过此框架聚合为一个 整个页面. h ...

  10. ******十三 ******、软设笔记【操作系统】-磁盘管理、虚设备与SPOOLing系统

    五.磁盘管理 1.磁盘的访问时间 *寻道时间Ts:把磁臂从当前位置移到指定磁道上所经历的时间 *选择延迟时间Tr:指定扇区移动到磁头下面所经历的时间. *传输时间Tt:数据从磁盘读出或向磁盘写入数据所 ...