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] ...
随机推荐
- TFS 签入时,提示“变更集注释策略 中的内部错误……”
提示: 变更集注释策略 中的内部错误.加载 变更集注释策略 策略时出错(策略程序集“Microsoft.TeamFoundation.PowerTools.CheckinPolicies.Change ...
- myeclipse部署maven时,src/main/resources里面配置文件加载不到webapp下classes路径下的问题
解决myeclipse部署maven时,src/main/resources里面配置文件加载不到webapp下classes路径下的问题. 有时候是src/main/resources下面的,有时候是 ...
- scala数据类型
# Scala数据类型 ## 1.数值类型 ### 1.1 与Java一样Scala也有8种数值类型 * Byte * Char * Short * Int * Long * Float * Doub ...
- QT 商业版调用activex插件
搭建好qt项目后 在main.cpp中写入如下代码 #include "test.h" #include <QtWidgets/QApplication> #inclu ...
- leetcode--Learn one iterative inorder traversal, apply it to multiple tree questions (Java Solution)
will show you all how to tackle various tree questions using iterative inorder traversal. First one ...
- (转)rename命令详解
rename命令详解: 原文:http://www.cnblogs.com/amosli/p/3491649.html 对文件重命名是常用的操作之一,一般对单个文件的重命名用mv命令,如: amosl ...
- IDEA/Eclipse安装 Alibaba Java Coding Guidelines 插件
为了让开发者更加方便.并且达到快速规范代码格式的目的并实行起来,阿里巴巴基于<阿里巴巴Java开发规约>手册内容,研发了一套自动化的IDE检测插件(IDEA.Eclipse).它就是Ali ...
- IO缓冲流
目录 IO缓冲流 缓冲流 基本原理 字节缓冲流 字符缓冲流 IO缓冲流 缓冲流也叫高效流,能够更高效的进行读取: 转换流:能够进行编码转换 序列化流:持久化存储对象 缓冲流 缓冲流--就是对应4个Fi ...
- 让javascript加载速度倍增的方法(解决JS加载速度慢的问题)
通常我们的网站里面会加载一些js代码,统计啊,google广告啊,百度同盟啊,阿里妈妈广告代码啊,一堆,最后弄得页面加载速度很慢,很慢. 解决办法:换一个js包含的方式,让javascript加载速度 ...
- 在 Angularjs 中 ui-sref 和 $state.go 如何传递单个多个参数和将对象作为参数
一: 如何传递单个参数 首先,要在目标页面定义接受的参数: 传参, 接收参数, 在目标页面的controller里注入$stateParams,然后 "$stateParams.参数名&qu ...