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] ...
随机推荐
- 默认约束 default
default :初始值设置,插入记录时,如果没有明确为字段赋值,则自动赋予默认值. 例子:create table tb6( id int primary key auto_increment ...
- BZOJ 3673 可持久化并查集 by zky && BZOJ 3674 可持久化并查集加强版 可持久化线段树
既然有了可持久化数组,就有可持久化并查集.. 由于上课讲过说是只能按秩合并(但是我也不确定...),所以就先写了按秩合并,相当于是维护fa[]和rk[] getf就是在这棵树中找,直到找到一个点的fa ...
- Models-查询详细操作
# 单表简单查询13种方法 1.all(): 查询所有结果 all: models.表名.objects.all() book_all=models.Book.objects.all() # 结果是q ...
- 高并发web系统优化总结
1.背景 因为业务需要,搭建了一个系统,系统主要由两部分组成,web页面和数据库. mysql大概2万条数据,其中有一个字段是click_num点击次数,php页面会取点击次数最小的一条记录去进行操作 ...
- mysql 日期与索引问题
日期类型可以直接和string格式的字符串比较 select * from xxx where event_time>'2018-06-02' 可以使用索引, mysql默认会把后面的字符串转成 ...
- 整理一下postgresql的扩展功能postgis和pgrouting的使用
postgis windows的下的安装使用postgresql的bin目录下的stackbuiler Ubuntu14.04下的安装: apt-get install postgresql-9.3- ...
- POJ 1410 Intersection 数据错误
题目要求判断一条线段和一个矩形是否相交,或者是否在矩形里面(题目好像没说?) 思路就是直接暴力判断和矩形四条边是否相交,和线段的坐标是否在矩形的坐标范围即可. 然后题目的数据,(xleft,ytop) ...
- C# 枚举与switch用法
using System; namespace Csharp { class Program { //枚举 public enum TimeOfDay { Morning=, Afternoon=, ...
- HBase基础讲解
HBase定义 HBase 是一个高可靠.高性能.面向列.可伸缩的分布式存储系统,利用Hbase技术可在廉价PC Server上搭建 大规模结构化存储集群. HBase 是 ...
- asp.net web 开发中配置web.config
一.配置数据库连接字符串 <connectionStrings> <add name="CaoLPractise" connectionString=" ...