当前位置:网站首页>188. 买卖股票的最佳时机 IV
188. 买卖股票的最佳时机 IV
2022-06-24 19:19:00 【anieoo】
原题链接:188. 买卖股票的最佳时机 IV
solution:
和买卖股票系列3是一样的方法,只不过交易次数为k次。
const int INF = 0x3f3f3f3f;
class Solution {
public:
int maxProfit(int k, vector<int>& prices) {
int n = prices.size();
if(n == 0) return 0;
vector<vector<vector<int>>> dp(n + 1, vector<vector<int>> (2, vector<int> (k + 1)));
dp[1][1][0] = -prices[0];
dp[1][0][0] = 0;
//第一天不可能完成交易
for(int i = 1;i <= k;i++)
dp[1][0][i] = -INF;
// 第一天不可能已经卖出
for(int i = 1;i <= k;i++)
dp[1][1][i] = -INF;
for(int i = 2;i <= n;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 - 1]);
}
dp[i][1][0] = max(dp[i - 1][1][0], dp[i - 1][0][0] - prices[i - 1]);
for(int j = 1;j <= k;j++) {
dp[i][1][j] = max(dp[i - 1][1][j], dp[i - 1][0][j] - prices[i - 1]);
}
}
int res = 0;
for(int i = 1;i <= k;i++){
res = max(res,dp[n][0][i]);
}
return res;
}
};
边栏推荐
- [performance tuning basics] performance tuning strategy
- Page replacement of virtual memory paging mechanism
- Pod lifecycle in kubernetes
- Sleep revolution - find the right length of rest
- Golang daily question
- Shrimp skin test surface treated
- Can the OPDS SQL component pass process parameters to the next component through context
- 大厂出海,败于“姿态”
- Combination mode -- stock speculation has been cut into leeks? Come and try this investment strategy!
- Axi DMA IP core operation process
猜你喜欢

Static routing job supplement

Microsoft Certification (dynamic 365) test

Builder mode -- Master asked me to refine pills
[email protected] -Perfmon metric collector listener steps"/>JMeter installation plug-in, adding [email protected] -Perfmon metric collector listener steps

Background operation retry gave up; KeeperErrorCode = ConnectionLoss

Appium desktop introduction

Leetcode (135) - distribute candy

JMeter response assertion

Oauth2.0 introduction

Memo mode - game archiving
随机推荐
Concepts of kubernetes components
Appium introduction and environment installation
[performance tuning basics] performance tuning standards
opds sql组件能不能将流程参数通过上下文传给下一个组件
Limit summary (under update)
Simple analysis of WordPress architecture
Typescript syntax
NPM download speed is slow
Leetcode (455) - distribute cookies
Requests requests for web page garbled code resolution
B站带货当学新东方
Handling of garbled JMeter response data - three solutions
Smooth live broadcast | analysis of key technologies for live broadcast pain points
Talking about the range of data that MySQL update will lock
Selenium crawl notes
Minimum cost and maximum flow (template question)
Why do we always "give up halfway"?
Web automation: summary of special scenario processing methods
After idea installs these plug-ins, the code can be written to heaven. My little sister also has to arrange it
Second understanding permutation and combination