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:
prices = [1, 2, 3, 0, 2]
maxProfit = 3
transactions = [buy, sell, cooldown, buy, sell]
- 解题思路 (原思路链接)
这道题目自己一开始根本没有想到用动态规划,菜鸟本色。之后看了dicussion区域排名第一的答案,也是云里雾里。
还好碰到了这个解决思路。这个方案把每天分为四个状态
- 有股票,卖 = 前天(有股票,休息)+ price 或者 前天(没股票,买)+ price
- 有股票,休息 = 前天(有股票,休息)或者 前天(没股票,买)
- 没股票,买 = 前天(没股票,休息)- price ==》 冷却期所以不可能是 有股票,卖
- 没股票,休息 = 前天(没股票,休息)或者 前天(有股票,卖)
结合这个思路,编码实现如下:
需要额外注意的一点是第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的更多相关文章
- 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 买卖股票的最佳时间有冷却期
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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 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 ...
- 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 ...
- 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(medium)
原题 思路: 状态转移 出售股票的状态,最大利润有两种可能. 一,和昨天一样不动:二,昨天持有的股票今天卖掉. sell[i] = max(sell[i-1],buy[i-1] + prices[i] ...
随机推荐
- HTTP的一些理解
URI是Uniform Resource Identifier的缩写,统一资源标识符.URI用字符串标识某一互联网资源,而URL标识资源的地点(互联网上所处的位置).可见URL是URI的子集. 典型的 ...
- ACM-ICPC 2018 南京赛区网络预赛 Lpl and Energy-saving Lamps (线段树:无序数组找到第一个小于val)
题意:n个房间,每个房间有ai盏旧灯,每个月可以买m盏新灯,要求:按房间顺序换灯,如果剩下的新灯数目大于ai,那么进行更换,否则跳过该房间,判断下一个房间.如果所有房间都换完灯,那么久不会再买新灯. ...
- 【Unity3D】用C#读取INI配置文件
由于在C#并没有提供专门读取INI文件的API,所以需要间接调用C++的API来进行读取. using System; using System.Collections.Generic; using ...
- LR C语言语句复习,几个简单代码
嵌套循环 Action() { int i,j; ;i<=;i++) { ) beark; else lr_output_message("i=%d",i); ;j<= ...
- jQuery 获取和设置表单元素
jQuery提供了val()方法,使用它我们可以快速地获取和设置表单的文本框.单选按钮.以及单选按钮的值. 使用val()不带参数,表示获取元素的值 使用val()给定参数,则表示把值赋给元素 如下: ...
- 360或其他双核浏览器下在兼容模式用chrome内核渲染的方法
<meta name="renderer" content="webkit"> <meta http-equiv="X-UA-COM ...
- Java的Map和Object之间的相互转换方法
public staic Map<String, Object> objectToMap(Object obj) throws Exception { if(obj == null) re ...
- springboot集成freemarker 配置application.properties详解
#配置freemarker详解 #spring.freemarker.allow-request-override=false # Set whether HttpServletRequest att ...
- 工作中遇到的有关echarts地图和百度地图的问题
工作中遇到的有关echarts地图和百度地图的问题 *** 前言:在做项目中需要制作一个场景是左边是柱状图,右边是地图,地图上悬浮一个按钮可以切换echarts地图和百度地图.*** 功能: 在点击左 ...
- <Android 应用 之路> 天气预报(四)
前言 第二次尝试完成天气预报应用,与上次不同的是,个人感觉这次的Ui不那么丑陋,整体的实用性和界面效果,用户体验相较上一次有所提升,但是还是有很多地方需要完善. 这次使用到的内容比较丰富,包括聚合数据 ...