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 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]
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
:
- you have 1 stock and you sell it
- you have 1 stock and you do nothing
- you have 0 stock and you buy stock
i
- 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:
- if you take action 1 on day
i
==> you have either taken action 2 or 3 on dayi-1
- if you take action 2 on day
i
==> you have either taken action 2 or 3 on dayi-1
- if you take action 3 on day
i
==> you must have taken action 4 on dayi-1
(you can not sell on dayi-1
due to cool down) - if you take action 4 on day
i
==> you have either taken action 1 or 4 on dayi-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的更多相关文章
- [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 (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 ...
- [leetcode] 309. Best Time to Buy and Sell Stock with Cooldown(medium)
原题 思路: 状态转移 出售股票的状态,最大利润有两种可能. 一,和昨天一样不动:二,昨天持有的股票今天卖掉. sell[i] = max(sell[i-1],buy[i-1] + prices[i] ...
- LeetCode 309 Best Time to Buy and Sell Stock with Cooldown 解决方案
题目描述 给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 . 设计一个算法计算出最大利润.在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票): 你不能同时参与多笔 ...
- 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】309. Best Time to Buy and Sell Stock with Cooldown 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 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 ...
- 【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 ...
随机推荐
- Python模块初识
目录 一 模块初识 二 模块分类 三 导入模块 四 Python文件的两种用途 五 模板查找顺序 六 软件开发目录规范 一.模块初识 模块是自我包含并且有组织的代码片段,是一系列功能的集合体,一个py ...
- php中加密和解密
项目要和第三方进行接口对接,所以数据的安全很重要.第一次自己设计并实现,学习记录下 网上查了很多资料,真的很深奥 对称加密: 双方共用一个约定好的密钥进行数据的加密和解密,但是当密匙丢失,数据将有泄露 ...
- css: position的使用;
position有四种模式: static, relative, position, fixed; 1.static(静态定位):默认值.没有定位,元素出现在正常的流中(忽略 top, bottom, ...
- QImage与cv::Mat转换;
QImage主要格式有QImage::Format_RGB32, QImage::Format_RGB888, QImage::Format_Index8, 不同的格式有不同的排布: 格式部分可以参考 ...
- maven编译或者打包web项目显示“软件包 javax.servlet.http 不存在"
2.解决办法: 这是由于缺少servlet-api.jar包,其实tomcat下有,但是在java build path把他加载过来,还是报这个错误,所以我们直接在pom.xml里面加入这个jar包即 ...
- beego 实现API自动化文档
安装beego和bee工具 1.beego安装 go get -u github.com/astaxie/beego 2.安装bee工具 go get -u github.com/beego/bee ...
- VirtualBox使用入门
VirtualBox使用入门 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 能玩虚拟机的人多少是懂点运维的,因此我就不跟大家介绍啥事虚拟化了.关于虚拟化产品大家用的应该也都大同小异 ...
- setAttribute和setParameter方法的区别
getAttribute表示从request范围取得设置的属性,必须要先setAttribute设置属性,才能通过getAttribute来取得,设置与取得的为Object对象类型 getParame ...
- WebRequest/HttpWebRequest/HttpRequest/WebClient/HttpClient的区别
1.WebRequest和HttpWebRequest WebRequest 的命名空间是: System.Net ,它是HttpWebRequest的抽象父类(还有其他子类如FileWebReque ...
- Web APi入门之Self-Host寄宿及路由原理
前言 刚开始表面上感觉Web API内容似乎没什么,也就是返回JSON数据,事实上远非我所想,不去研究不知道,其中的水还是比较深,那又如何,一步一个脚印来学习都将迎刃而解. Self-Host 我们知 ...