当前位置:网站首页>动态规划股票问题对比
动态规划股票问题对比
2022-07-04 15:39:00 【折叠的饼干】
文章目录
核心还是状态转移,只要搞清每个状态是什么就非常简单
搞不清楚转移过程可以打表看看
这里从dp[i][j],i从1开始是因为习惯性想避开i-1导致越界
1
121. 买卖股票的最佳时机
给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。
你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。
返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。
剑指 Offer 63. 股票的最大利润
买卖该股票一次可能获得的最大利润是多少?
解析:
这两个是同一道题,都是只能买一次卖一次
下面两种方法只是思考角度不同:
1.dp[i][0]和dp[i][1]分别代表无股票(可能是没买,可能是卖了)、有股票(只能买一次,所以不能叠加上次值)两种状态
%2是个节省空间的方法,可以不加
2.
dp[i][0]未买过
dp[i][1]买过一次,且未卖出
dp[i][2]买过一次,且卖出
class Solution {
public:
int maxProfit(vector<int>& prices) {
int len=prices.size();
vector<vector<int>>dp(2,vector<int>(2,0));
int ans=0;
dp[1][0]=0;
dp[1][1]=-prices[0];
for(int i=2;i<=len;i++){
dp[i%2][0]=max(dp[(i-1)%2][0],dp[(i-1)%2][1]+prices[i-1]);
dp[i%2][1]=max(dp[(i-1)%2][1],-prices[i-1]);
}
return dp[len%2][0];
}
};
class Solution {
public:
int maxProfit(vector<int>& prices) {
int len=prices.size();
if(len==0)return 0;
vector<vector<int>>dp(len+1,vector<int>(3,0));
dp[1][1]=-prices[0];
for(int i=2;i<=len;i++){
dp[i][0]=dp[i-1][0];
dp[i][1]=max(dp[i-1][0]-prices[i-1],dp[i-1][1]);
dp[i][2]=max(dp[i-1][1]+prices[i-1],dp[i-1][2]);
}
return dp[len][2];
}
};
2
122. 买卖股票的最佳时机 II
给你一个整数数组 prices ,其中 prices[i] 表示某支股票第 i 天的价格。
在每一天,你可以决定是否购买和/或出售股票。你在任何时候 最多 只能持有 一股 股票。你也可以先购买,然后在 同一天 出售。
返回 你能获得的 最大 利润 。
解析:
和1相比,可以多次买入卖出,意思就是买入可以叠加上次状态了
class Solution {
public:
int maxProfit(vector<int>& prices) {
int len=prices.size();
vector<vector<int>>dp(len+1,vector<int>(2,0));
dp[1][1]=-prices[0];
dp[1][0]=0;
for(int i=2;i<=len;i++){
dp[i][0]=max(dp[i-1][0],dp[i-1][1]+prices[i-1]);
dp[i][1]=max(dp[i-1][1],dp[i-1][0]-prices[i-1]);
}
return dp[len][0];
}
};
3
123. 买卖股票的最佳时机 III
给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。
设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。
注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
解析:
和2相比,限制买入次数,只能买入两次
就用5个状态标记
dp[i][0] 未买过
dp[i][1] 买过一次,并持有
dp[i][2] 买过一次,并卖了
dp[i][3]买过两次,并持有
dp[i][4]买过两次,并卖了
注意初始化,可以一天买了又卖又买
class Solution {
public:
int maxProfit(vector<int>& prices) {
int len=prices.size();
vector<vector<int>>dp(len+1,vector<int>(5,0));
dp[1][1]=-prices[0];
//注意一天可以买卖两次
dp[1][3]=-prices[0];
for(int i=2;i<=len;i++){
dp[i][0]=dp[i-1][0];
dp[i][1]=max(dp[i-1][0]-prices[i-1],dp[i-1][1]);
dp[i][2]=max(dp[i-1][1]+prices[i-1],dp[i-1][2]);
dp[i][3]=max(dp[i-1][2]-prices[i-1],dp[i-1][3]);
dp[i][4]=max(dp[i-1][3]+prices[i-1],dp[i-1][4]);
}
return dp[len][4];
}
};
4
188. 买卖股票的最佳时机 IV
给定一个整数数组 prices ,它的第 i 个元素 prices[i] 是一支给定的股票在第 i 天的价格。
设计一个算法来计算你所能获取的最大利润。你最多可以完成 k 笔交易。
注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)
解析:
和3相比,买入两次变k次
就用2k+1个状态标记
dp[i][0] 未买过
dp[2i+1][1] 买过一次,并持有
dp[ik+2][2] 买过一次,并卖了
然后一循环就完了
注意初始化,可以一天买了又卖又买又…
class Solution {
public:
int maxProfit(int k, vector<int>& prices) {
int len=prices.size();
if(len==0)return 0;
vector<vector<int>>dp(len+1,vector<int>(2*k+1,0));
for(int i=0;i<k;i++){
dp[1][2*i+1]=-prices[0];
}
for(int i=2;i<=len;i++){
dp[i][0]=dp[i-1][0];
for(int j=0;j<k;j++){
dp[i][2*j+1]=max(dp[i-1][2*j]-prices[i-1],dp[i-1][2*j+1]);
dp[i][2*j+2]=max(dp[i-1][2*j+1]+prices[i-1],dp[i-1][2*j+2]);
}
}
return dp[len][2*k];
}
};
5
714. 买卖股票的最佳时机含手续费
给定一个整数数组 prices,其中 prices[i]表示第 i 天的股票价格 ;整数 fee 代表了交易股票的手续费用。
你可以无限次地完成交易,但是你每笔交易都需要付手续费。如果你已经购买了一个股票,在卖出它之前你就不能再继续购买股票了。
返回获得利润的最大值。
注意:这里的一笔交易指买入持有并卖出股票的整个过程,每笔交易你只需要为支付一次手续费。
解析:
跟2相比,在卖出时-fee就行
class Solution {
public:
int maxProfit(vector<int>& prices, int fee) {
int len=prices.size();
vector<vector<int>>dp(len+1,vector<int>(2,0));
dp[1][1]=-prices[0];
for(int i=2;i<=len;i++){
dp[i][0]=max(dp[i-1][0],dp[i-1][1]+prices[i-1]-fee);
dp[i][1]=max(dp[i-1][1],dp[i-1][0]-prices[i-1]);
}
return dp[len][0];
}
};
边栏推荐
- Difference between redis' memory obsolescence strategy and expiration deletion strategy
- The 18th IET AC / DC transmission International Conference (acdc2022) was successfully held online
- 《吐血整理》保姆级系列教程-玩转Fiddler抓包教程(2)-初识Fiddler让你理性认识一下
- 安信证券网上开户安全吗 开户收费吗
- Is it safe for CITIC Securities to open an account online? Is the account opening fee charged
- [Acwing] 58周赛 4489. 最长子序列
- Datakit -- the real unified observability agent
- 被PMP考试“折磨”出来的考试心得,值得你一览
- How to contribute to the source code of ongdb core project
- Learn more about the basic situation of 2022pmp examination
猜你喜欢
【Go ~ 0到1 】 第六天 文件的读写与创建
leetcode:421. 数组中两个数的最大异或值
昆明三环闭合工程将经过这些地方,有在你家附近的吗?
Understand ThreadLocal in one picture
Readis configuration and optimization of NoSQL (final chapter)
Congratulations to Mr. Zhang Pengfei, chief data scientist of artefact, for winning the campaign Asia tech MVP 2022
The 18th IET AC / DC transmission International Conference (acdc2022) was successfully held online
居家打工年入800多万,一共五份全职工作,他还有时间打游戏
Learn more about the basic situation of 2022pmp examination
VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
随机推荐
CANN算子:利用迭代器高效实现Tensor数据切割分块处理
Why do you say that the maximum single table of MySQL database is 20million? Based on what?
Object. Usage of keys()
How can programmers improve the speed of code writing?
建筑建材行业经销商协同系统解决方案:赋能企业构建核心竞争力
2022PMP考试基本情况详情了解
S2b2b solution for lighting industry: efficiently enable the industrial supply chain and improve the economic benefits of enterprises
MVC模式和三层架构
Using win10 scheduling task program to automatically run jar package at fixed time
Median and order statistics
[Acwing] 58周赛 4489. 最长子序列
kaili不能输入中文怎么办???
World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
Solution du système de gestion de la chaîne d'approvisionnement du parc logistique intelligent
Overflow: the combination of auto and Felx
Go语言循环语句(第10课下)
[acwing] 58 weeks 4489 Longest subsequence
The 18th IET AC / DC transmission International Conference (acdc2022) was successfully held online
居家打工年入800多万,一共五份全职工作,他还有时间打游戏
Visual Studio 2019 (LocalDB)MSSQLLocalDB SQL Server 2014 数据库版本为852无法打开,此服务器支持782