当前位置:网站首页>LeetCode 188. The best time to buy and sell stocks iv*** (double, need triple)
LeetCode 188. The best time to buy and sell stocks iv*** (double, need triple)
2022-07-18 06:36:00 【Evening rain forest bell】
Specific ideas :
123 Expansion of the topic
Specific code :
class Solution {
public:
int maxProfit(int k, vector<int>& prices) {
if(k==0||prices.size()==0)
return 0;
int inf=-100000;
int n=prices.size();
vector<vector<vector<int>>>dp(n,vector<vector<int>>(2,vector<int>(k+1,0)));
for(int i=1;i<=k;i++){
dp[0][0][i]=dp[0][1][i]=inf;
}
dp[0][1][0]=-prices[0];
for(int i=1;i<n;i++){
dp[i][0][0]=0;
dp[i][1][0]=max(dp[i-1][1][0],dp[i-1][0][0]-prices[i]);
for(int j=1;j<=k;j++){
dp[i][0][j]=max(dp[i-1][0][j],dp[i-1][1][j-1]+prices[i]);
dp[i][1][j]=max(dp[i-1][1][j],dp[i-1][0][j]-prices[i]);
}
dp[i][1][k]=inf;
}
int ret=0;
for(int i=0;i<=k;i++){
ret=max(ret,dp[n-1][0][i]);
}
return ret;
}
};
边栏推荐
猜你喜欢
随机推荐
flink.14 DataStream模块 source底层是怎么实现的?
Hongke case | nanogue uses Onyx system to realize nondestructive characterization of graphene electrical properties
Matlab-mex
Face the object
SSM整合出现问题- org.apache.ibatis.transaction.TransactionFactory
論文中的好文佳句摘錄
MySQL数据库安装&问题
Together with Alibaba cloud, grafana labs will provide the first grafana hosting service in China
leetcode:330. 按要求补齐数组
The INI file configuration should be modified during Jerry's mode [chapter]
SSM integration problems - org apache. ibatis. transaction. TransactionFactory
Function advanced application
leetcode:330. Complete the array as required
QT UI Designer interface common operation records (qtablewidget)
圖撲 Web 可視化引擎在仿真分析領域的應用
After reading these five reasons, I finally know the reason why FTP was replaced
面对对象
Practice of online problem feedback module (II): automatic generation of class file by encapsulating code
@EqualsAndHashCode注解的使用
What app should individuals use to buy stocks is safer and faster








