当前位置:网站首页>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);
}
};
边栏推荐
- Station B takes goods to learn from New Oriental
- Pyaudio audio recording
- Auto. JS to automatically authorize screen capture permission
- Static routing experiment
- Arkit与Character Creator动画曲线的对接
- Dynamic routing protocol rip, OSPF
- Functional analysis of ebpf sockops
- Summary of message protocol problems
- Shell script
- Static routing job
猜你喜欢
Network flow 24 questions (round table questions)
使用 Go 编程语言 66 个陷阱:Golang 开发者的陷阱和常见错误指北
Football information query system based on C language course report + project source code + demo ppt+ project screenshot
Alibaba cloud schedules tasks and automatically releases them
基于STM32的物联网下智能化养鱼鱼缸控制控制系统
EditText controls the soft keyboard to search
memcached全面剖析–5. memcached的应用和兼容程序
Memcached comprehensive analysis – 2 Understand memcached memory storage
Static routing experiment
Realization of truth table assignment by discrete mathematical programming
随机推荐
Talking about the range of data that MySQL update will lock
Minimum cost and maximum flow (template question)
Sleep revolution - find the right length of rest
VirtualBox virtual machine installation win10 Enterprise Edition
EditText controls the soft keyboard to search
Wireshark packet capturing skills summarized by myself
Tutorial on obtaining JD cookies by mobile browser
OSI and tcp/ip model
66 pitfalls in go programming language: pitfalls and common errors of golang developers
Call process of package receiving function
CondaValueError: The target prefix is the base prefix. Aborting.
Microsoft Certification (dynamic 365) test
yeb_ Back first day
DHCP operation
Please open online PDF carefully
Remember the frequently forgotten problem of continuously reading pictures -%04d
Pyaudio audio recording
Analyse complète Memcached – 2. Comprendre le stockage de mémoire pour Memcached
Time standard and format
Role of wait function