当前位置:网站首页>Dynamic planning for solving problems (5)
Dynamic planning for solving problems (5)
2022-07-27 06:09:00 【RL-UAV】
21. The best time to buy and sell stocks
class Solution {
public:
int maxProfit(vector<int>& prices) {
int len = prices.size();
if (len == 0) return 0;
vector<vector<int>> dp(len, vector<int>(2));
dp[0][0] -= prices[0];
dp[0][1] = 0;
for (int i = 1; i < len; i++) {
dp[i][0] = max(dp[i - 1][0], -prices[i]);
dp[i][1] = max(dp[i - 1][1], prices[i] + dp[i - 1][0]);
}
return dp[len - 1][1];
}
};
122. The best time to buy and sell stocks II
class Solution {
public:
int maxProfit(vector<int>& prices) {
int len = prices.size();
vector<vector<int>> dp(len, vector<int>(2, 0));
dp[0][0] -= prices[0];
dp[0][1] = 0;
for (int i = 1; i < len; i++) {
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i]); // Notice here is and 121. The only difference between the best time to buy and sell stocks .
dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i]);
}
return dp[len - 1][1];
}
};
- Time complexity :O(n)
- Spatial complexity :O(n)
123. The best time to buy and sell stocks III
dpi in i It means the first one i God ,j by [0 - 4] Five states ,dpi It means the first one i Day state j The biggest cash left .
// Version of a
class Solution {
public:
int maxProfit(vector<int>& prices) {
if (prices.size() == 0) return 0;
vector<vector<int>> dp(prices.size(), vector<int>(5, 0));
dp[0][1] = -prices[0];
dp[0][3] = -prices[0];
for (int i = 1; i < prices.size(); i++) {
dp[i][0] = dp[i - 1][0];
dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
dp[i][2] = max(dp[i - 1][2], dp[i - 1][1] + prices[i]);
dp[i][3] = max(dp[i - 1][3], dp[i - 1][2] - prices[i]);
dp[i][4] = max(dp[i - 1][4], dp[i - 1][3] + prices[i]);
}
return dp[prices.size() - 1][4];
}
};
188. The best time to buy and sell stocks IV
So in the same way, we can deduce dp0 When j When it is an odd number, it is initialized to -prices[0]
initialization All worth , The biggest difference is the analogy here j Odd is to buy , Even numbers are sold .
class Solution {
public:
int maxProfit(int k, vector<int>& prices) {
if (prices.size() == 0) return 0;
vector<vector<int>> dp(prices.size(), vector<int>(2 * k + 1, 0));
for (int j = 1; j < 2 * k; j += 2) {
dp[0][j] = -prices[0];
}
for (int i = 1;i < prices.size(); i++) {
for (int j = 0; j < 2 * k - 1; j += 2) {
dp[i][j + 1] = max(dp[i - 1][j + 1], dp[i - 1][j] - prices[i]);
dp[i][j + 2] = max(dp[i - 1][j + 2], dp[i - 1][j + 1] + prices[i]);
}
}
return dp[prices.size() - 1][2 * k];
}
};
309. The best time to buy and sell stocks includes the freezing period
summary : existing 2 A buy sell Status of holding shares , Only then Buy today Selling status today .
- State one : Buy stock status ( Buy stocks today , Or you bought stocks before and didn't operate )
- Status of selling shares , There are two states of selling stocks
- State two : Sold the stock two days ago , Passed the freezing period , Never operated , Keep selling stocks today
- State three : I sold shares today
- State four : Today is in a frozen state , But the frozen state is unsustainable , Only one day !
Incomprehensible state ,
- State one : Buy stock status ( Buy stocks today , Or you bought stocks before and didn't operate )
- Status of selling shares , There are two states of selling stocks
- State two : Sold the stock two days ago , Passed the freezing period , Never operated , Keep selling stocks today
- State three : I sold shares today
- State four : Today is in a frozen state , But the frozen state is unsustainable , Only one day !
Write the wrong code :
dp[i][0] = max(dp[i - 1][0], max(dp[i - 1][3], dp[i - 1][1]) - prices[i]); //max(dp[i - 2][3]
max(dp[i - 1][3], dp[i - 1][1]) - prices[i]);
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
if (n == 0) return 0;
vector<vector<int>> dp(n, vector<int>(4, 0));
dp[0][0] -= prices[0]; // Holding shares
for (int i = 1; i < n; i++) {
dp[i][0] = max(dp[i - 1][0], max(dp[i - 1][3], dp[i - 1][1]) - prices[i]);
dp[i][1] = max(dp[i - 1][1], dp[i - 1][3]);
dp[i][2] = dp[i - 1][0] + prices[i];
dp[i][3] = dp[i - 1][2];
}
return max(dp[n - 1][3],max(dp[n - 1][1], dp[n - 1][2]));
}
};
714. The best time to buy and sell stocks includes handling fees
The difference is that there needs to be an additional operation to deduct the handling fee .
class Solution {
public:
int maxProfit(vector<int>& prices, int fee) {
int n = prices.size();
vector<vector<int>> dp(n, vector<int>(2, 0));
dp[0][0] -= prices[0]; // Holding shares
for (int i = 1; i < n; i++) {
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i] - fee);
}
return max(dp[n - 1][0], dp[n - 1][1]);
}
};
- Time complexity :O(n)
- Spatial complexity :O(n)
边栏推荐
- geonode geoserver win10 安装教程(亲测)
- Weidongshan digital photo frame project learning (II) displaying Chinese characters on LCD
- LaTeX中多个公式公用一个序号时
- 2021-06-26
- 力扣题解 动态规划(2)
- arcgis for js api-入门系列
- 力扣每日一题 剑指 Offer II 091. 粉刷房子
- 编程学习记录——第9课【操作符】
- Code implementation and introduction of all commonly used sorting
- Unity Shader 概述
猜你喜欢

PZK学C语言之数据类型,进制转换,输入输出,运算符,分支语句ifelse

Unity 引擎开始从 Mono 迁移到 .NET CoreCLR

非重叠矩形中的随机点(力扣每日一题)

Kaggle调用自定义模块方法

UnityShader-LowPoly
![[first song] machine learning of rebirth - linear regression](/img/70/3efd9eacf88f55022eb52d096926f7.png)
[first song] machine learning of rebirth - linear regression

UnityShader-高斯模糊

Code implementation and introduction of all commonly used sorting

Unity Hub登录无响应

Weidongshan digital photo frame project learning (IV) simple TXT document display (e-paper book)
随机推荐
1半自动爬虫
编程学习记录——第3课【初识C语言】
编程学习记录——第8课【数组与设计五子棋,扫雷游戏】
力扣题解 二叉树(5)
Live Home 3D Pro interior home design tool
力扣题解 动态规划(3)
剪枝-量化-转onnx中文系列教程
发布 分辨率0.22m的建筑物分割数据库
Force buckle 110. Balanced binary tree
arcgis for js api(2) 获取要素服务的id集合
C语言-文件操作
哈希表的原理及哈希冲突的解决方法
文件的路径
安全帽反光衣检测识别数据集和yolov5模型
[first song] Introduction to data science of rebirth -- return to advanced level
SQL初识
力扣题解 单调栈
判断是否为回文结构的三种方法
使用Markdowm
Man moon myth reading notes