LeetCode 309 Best Time to Buy and Sell Stock with Cooldown 解决方案
题目描述
给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 。
设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):
- 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
- 卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天)。
示例:
输入: [1,2,3,0,2]
输出: 3
解释: 对应的交易状态为: [买入, 卖出, 冷冻期, 买入, 卖出]
解决方案1
可以通过一个状态转换图,来解决。
定义状态
s0 : 没有买股票时
s1 :买入股票且没有卖出股票时
s2:卖出股票是
那么状态转换可以这样表示
so->s1 买股票 s0->s0不操作
s1->s2 卖股票 s1->s1不操作
s2->s0 经过冷冻期 s2->s2 不操作
转换图也可以画出来:
那么每一次价格变动都会在这三个状态里
得出递推式
s0[i] = max(s0[i - 1], s2[i - 1]); 不操作 和 冷冻期过去
s1[i] = max(s1[i - 1], s0[i - 1] - prices[i]); 不操作 和 买股票
s2[i] = s1[i - 1] + prices[i]; 卖出股票
最终的状态应该会在s0和s2终态度有关, 因为s1是卖出股票 是 会比s0小的,
最优解表示为 max(s0[最后一次价格变动],s2[最后一次价格变动])
具体代码如下;
class Solution {
public:
int maxProfit(vector<int>& prices){
if (prices.size() <= 1) return 0;
vector<int> s0(prices.size(), 0);
vector<int> s1(prices.size(), 0);
vector<int> s2(prices.size(), 0);
//初始化
s1[0] = -prices[0];
s0[0] = 0;
s2[0] = INT_MIN;
//状态转移
for (int i = 1; i < prices.size(); i++) {
s0[i] = max(s0[i - 1], s2[i - 1]);
s1[i] = max(s1[i - 1], s0[i - 1] - prices[i]);
s2[i] = s1[i - 1] + prices[i];
}
return max(s0[prices.size() - 1], s2[prices.size() - 1]);
}
};
总结
这个动态规划问题竟然能用一种状态转换的方式来得到也是很神奇了, 动态规划关键是找到此时最优解和上一次的最优解之间的关系,而这个题目最优解是有2个状态产生的,而状态直接又有着紧密的关系,所以用状态转换图来理清关系,找到最优解的更新表示,最终得到结果。还是值得深思的。
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
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 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 ...
随机推荐
- react实战 : react 与 canvas
有一个需求是这样的. 一个组件里若干个区块.区块数量不定. 区块里面是一个正六边形组件,而这个用 SVG 和 canvas 都可以.我选择 canvas. 所以就变成了在 react 中使用 canv ...
- ionic环境安装步骤
注:准确性有待考证,仅供参考. 1,安装jdk 配置环境变量:java_home和path2,安装node 检查版本 node -v3,安装npm:npm i cnpm -g 检查版本:cnpm -v ...
- 167两数之和II-输入有序数组
from typing import List# 这道题很容易能够想到,只需要遍历两边列表就可以了# 两层循环class Solution: def twoSum(self, numbers: Lis ...
- 题解 洛谷 P2046 【[NOI2010]海拔】
首先进行贪心,发现海拔有梯度时一定是不优的,最优的情况是海拔像断崖一样上升,也就是左上角有一片海拔高度为\(0\),右下角有一片海拔高度为\(1\). 发现这样的性质后,不难想到用最小割来解决问题,但 ...
- awk格式化
1,获取指定列和行,在指定列和行内插入 指定符号. /p/v2/api/winapi/mini/merchant/admin/notice/list/1/10?current=1&size=1 ...
- Ansible 远程服务器连接 Internet 测试
Email 连接: lonnyliu@126.com 需求 在使用Ansible过程中不可避免需要了解远端服务器是否能够联通外网,以便进行在线安装软件或者其他.对于运维人员来说普遍的办法有 1. 登录 ...
- 删除GIT中的.DS_Store
转载自:https://www.jianshu.com/p/fdaa8be7f6c3 .DS_Store 是什么 使用 Mac 的用户可能会注意到,系统经常会自动在每个目录生成一个隐藏的 .DS_St ...
- smartSVN9.2.2 for mac 安装与破解
原文链接:https://www.jianshu.com/p/bb87154e0459 近段时间使用svn进行项目管理,开始使用的是cornerstone,但是用过程中出现一个操作Bug,一.在xco ...
- Springboot 多数据源配置,结合tk-mybatis
一.前言 作为一个资深的CRUD工程师,我们在实际使用springboot开发项目的时候,难免会遇到同时使用多个数据库的情况,比如前脚刚查询mysql,后脚就要查询sqlserver. 这时,我们很直 ...
- PHP curl_version函数
(PHP 5 >= 5.5.0) curl_version — 获取cURL版本信息. 说明 array curl_version ([ int $age = CURLVERSION_NOW ] ...