当前位置:网站首页>188. the best time to buy and sell stocks IV
188. the best time to buy and sell stocks IV
2022-06-24 21:28:00 【anieoo】
Original link :188. The best time to buy and sell stocks IV
solution:
And trading stock series 3 It's the same way , Only the number of transactions is k Time .
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;
// It is impossible to complete the transaction on the first day
for(int i = 1;i <= k;i++)
dp[1][0][i] = -INF;
// It can't have been sold on the first day
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;
}
};
边栏推荐
- Splicing audio files with ffmpeg-4.3
- Different WordPress pages display different gadgets
- Three more days
- (to be optimized and modified) vivado DDR4 SDRAM (MIG) (2.2) IP core learning record
- OSI notes sorting
- RFC 793 why to send reset and when to send reset
- 66 pitfalls in go programming language: pitfalls and common errors of golang developers
- [cloud native learning notes] kubernetes practice command
- Capture the whole process of accessing web pages through Wireshark
- Appium introduction and environment installation
猜你喜欢

Big factories go out to sea and lose "posture"

Sleep revolution - find the right length of rest

Adding subscribers to a list using mailchimp's API V3

Capture the whole process of accessing web pages through Wireshark

Wireshark packet capturing skills summarized by myself

EditText 控制软键盘出现 搜索

Summary of message protocol problems

188. 买卖股票的最佳时机 IV

VirtualBox virtual machine installation win10 Enterprise Edition

Dynamic routing protocol rip, OSPF
随机推荐
C语言实现DNS请求器
188. 买卖股票的最佳时机 IV
Rename and delete files
Splicing audio files with ffmpeg-4.3
Shell script
Background operation retry gave up; KeeperErrorCode = ConnectionLoss
大厂出海,败于“姿态”
Handwritten RPC the next day -- review of some knowledge
memcached全面剖析–2. 理解memcached的內存存儲
Physical layer introduction
Call process of package receiving function
Learn to use a new technology quickly
What does CTO (technical director) usually do?
CondaValueError: The target prefix is the base prefix. Aborting.
Network security review office starts network security review on HowNet
RFC 793 why to send reset and when to send reset
Read all text from stdin to a string
Memcached full profiling – 1 Fundamentals of memcached
VirtualBox virtual machine installation win10 Enterprise Edition
Go coding specification