当前位置:网站首页>123. the best time to buy and sell shares III
123. the best time to buy and sell shares III
2022-06-24 21:28:00 【anieoo】
Original link :123. The best time to buy and sell stocks III
solution:
Dynamic programming :
State means :dp[i][j][k] Express dp[ Days ][ Whether or not you own shares ][ Number of transactions completed ] Maximum profit earned .
State shift : Already commented in the code
const int INF = 0x3f3f3f3f;
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
// Define 3D array
//dp[i][j][k] Express dp[ Days ][ Whether or not you own shares ][ Number of transactions ] Maximum profit obtained
vector<vector<vector<int>>> dp(n + 1, vector<vector<int>> (2, vector<int> (3, 0)));
dp[1][1][0] = -prices[0];
dp[1][0][0] = 0;
// It is impossible to complete the transaction on the first day
dp[1][0][1] = -INF;
dp[1][0][2] = -INF;
// It can't have been sold on the first day
dp[1][1][1] = -INF;
dp[1][1][2] = -INF;
for(int i = 2;i <= n;i++) {
dp[i][0][0] = 0; // No shares , No transaction , Profit is 0.
// The maximum value of one transaction has been transferred from the stocks held but not traded on the previous day and the stocks not held on the previous day
dp[i][0][1] = max(dp[i - 1][1][0] + prices[i - 1], dp[i - 1][0][1]);
// The trading of stocks held on the previous day and the trading of stocks not held on the previous day have been completed 2 Transfer of maximum value of transactions
dp[i][0][2] = max(dp[i - 1][1][1] + prices[i - 1], dp[i - 1][0][2]);
// Transfer from the maximum value of stocks not held in the previous day to the maximum value of stocks purchased today and held in the previous day
dp[i][1][0] = max(dp[i - 1][0][0] - prices[i - 1], dp[i - 1][1][0]);
// Complete a transaction by not holding shares in the previous day, purchase today and complete an education maximum transfer by holding shares in the previous day
dp[i][1][1] = max(dp[i - 1][0][1] - prices[i - 1], dp[i - 1][1][1]);
dp[i][1][2] = -INF;
}
return max(max(dp[n][0][2], dp[n][0][1]), 0);
}
};边栏推荐
- Three more days
- Kernel Debugging Tricks
- Decoration home page custom full screen video playback effect GIF dynamic picture production video tutorial playback code operation settings full screen center Alibaba international station
- regular expression
- Tso hardware sharding is a header copy problem
- data link layer
- 自己总结的wireshark抓包技巧
- Common member methods of the calendar class
- Arkit与Character Creator动画曲线的对接
- [cloud native learning notes] learn about kubernetes' pod
猜你喜欢

Memcached comprehensive analysis – 2 Understand memcached memory storage

Oauth1.0 introduction

使用 Go 编程语言 66 个陷阱:Golang 开发者的陷阱和常见错误指北

Appium introduction and environment installation

memcached完全剖析–1. memcached的基础

Tutorial on obtaining JD cookies by mobile browser

Create a multithreaded thread class
![[cloud native learning notes] kubernetes Foundation](/img/c9/bd9ac90dd0dd27c450db33ad6b2fa5.jpg)
[cloud native learning notes] kubernetes Foundation

EditText 控制软键盘出现 搜索

Record a deletion bash_ Profile file
随机推荐
Appium introduction and environment installation
When to send the update windows message
Docking of arkit and character creator animation curves
Failed to open after installing Charles without any prompt
memcached完全剖析–1. memcached的基础
Web automation: web control interaction / multi window processing / Web page frame
Self signed certificate generation
TCP_ Nodelay and TCP_ CORK
SYSCALL_ Define5 setsockopt code flow
Golang reflection operation collation
Analysis of BBR congestion control state machine
yeb_ Back first day
GDB debugging
List set Introduction & common methods
大厂出海,败于“姿态”
The first day of handwritten RPC -- review of some basic knowledge
Subnet partition operation
推荐模型之多任务模型:ESMM、MMOE
Unity关于本地坐标和世界坐标之间的转换
how to install clustershell