当前位置:网站首页>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);
}
};边栏推荐
- Auto. JS to automatically authorize screen capture permission
- 基于STM32的物联网下智能化养鱼鱼缸控制控制系统
- Axi DMA IP core operation process
- A/b test helps the growth of game business
- 图的邻接表存储 数组实现
- Php-pdo parameter binding problem
- Pod lifecycle in kubernetes
- Requests requests for web page garbled code resolution
- Three more days
- Does the developer want to change to software testing?
猜你喜欢

EditText 控制软键盘出现 搜索

Role of wait function

JMeter implementation specifies concurrent loop testing

Pytest test framework II

Auto. JS to automatically authorize screen capture permission

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

虚拟货币7个月蒸发2万亿美元,“马斯克们”终结15万人暴富梦

Appium desktop introduction

Analysis of BBR congestion control state machine

Address mapping of virtual memory paging mechanism
随机推荐
TCP specifies the source port
Analysis of errors in JSON conversion using objectmapper
Web automation: web control interaction / multi window processing / Web page frame
HCIA assessment
Static routing job supplement
Nifi fast authentication configuration
Big factories go out to sea and lose "posture"
Three more days
Golang reflection operation collation
ping: www.baidu.com: 未知的名称或服务
Read all text from stdin to a string
去掉录屏提醒(七牛云demo)
Network flow 24 questions (round table questions)
Introduction to interval DP
关于Unity中的transform.InverseTransformPoint, transform.InverseTransofrmDirection
Oauth1.0 introduction
Foundations of Cryptography
CondaValueError: The target prefix is the base prefix. Aborting.
After 5 months' test, it took 15K to come for an interview. When I asked, it was not worth even 5K. It was really
装修首页自定义全屏视频播放效果gif动态图片制作视频教程播放代码操作设置全屏居中阿里巴巴国际站