当前位置:网站首页>力扣题解 动态规划(5)
力扣题解 动态规划(5)
2022-07-27 05:20:00 【RL-UAV】
21. 买卖股票的最佳时机
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.买卖股票的最佳时机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]); // 注意这里是和121. 买卖股票的最佳时机唯一不同的地方。
dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i]);
}
return dp[len - 1][1];
}
};
- 时间复杂度:O(n)
- 空间复杂度:O(n)
123.买卖股票的最佳时机III
dpi中 i表示第i天,j为 [0 - 4] 五个状态,dpi表示第i天状态j所剩最大现金。
// 版本一
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.买卖股票的最佳时机IV
所以同理可以推出dp0当j为奇数的时候都初始化为 -prices[0]
初始化 所有值,最大的区别就是这里要类比j为奇数是买,偶数是卖的状态。
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.最佳买卖股票时机含冷冻期
总结 :现有2个买入 卖出 持有股票状态,才有 今天买入 今天卖出状态。
- 状态一:买入股票状态(今天买入股票,或者是之前就买入了股票然后没有操作)
- 卖出股票状态,这里就有两种卖出股票状态
- 状态二:两天前就卖出了股票,度过了冷冻期,一直没操作,今天保持卖出股票状态
- 状态三:今天卖出了股票
- 状态四:今天为冷冻期状态,但冷冻期状态不可持续,只有一天!
看不懂的状态,
- 状态一:买入股票状态(今天买入股票,或者是之前就买入了股票然后没有操作)
- 卖出股票状态,这里就有两种卖出股票状态
- 状态二:两天前就卖出了股票,度过了冷冻期,一直没操作,今天保持卖出股票状态
- 状态三:今天卖出了股票
- 状态四:今天为冷冻期状态,但冷冻期状态不可持续,只有一天!
写错代码部分:
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]; // 持股票
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.买卖股票的最佳时机含手续费
区别就是这里需要多一个减去手续费的操作。
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]; // 持股票
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]);
}
};
- 时间复杂度:O(n)
- 空间复杂度:O(n)
边栏推荐
- 【头歌】重生之我在py入门实训中(10): Numpy
- 物联网操作系统
- Weidongshan digital photo frame project learning (I) display ASCII characters on LCD
- [song] rebirth of me in py introductory training (7): function call
- 对于windows下的Redis,只能读不能写的问题
- [first song] machine learning of rebirth - linear regression
- 代码随想录笔记_哈希_242有效的字母异位词
- Chrome 如何快速将一组正在浏览的网页(tabs)转移到另一台设备(电脑)上
- 力扣题解 二叉树(6)
- 安全帽反光衣检测识别数据集和yolov5模型
猜你喜欢

剪枝-量化-转onnx中文系列教程

Redis在windows下的idea连接不上问题

PS 2022 updated in June, what new functions have been added

【11】二进制编码:“手持两把锟斤拷,口中疾呼烫烫烫”?

制作视频后期特效需要什么工具?

geonode geoserver win10 安装教程(亲测)

Speech and Language Processing (3rd ed. draft) Chapter 2 ——正则表达式,文本归一化,编辑距离 阅读笔记

Lightroom classic 2022 v11.4 Chinese version "latest resources"

Chrome 如何快速将一组正在浏览的网页(tabs)转移到另一台设备(电脑)上

STM32-FSMC外扩内存SRAM
随机推荐
geonode geoserver win10 安装教程(亲测)
李宏毅 2020 深度学习与人类语言处理 DLHLP-Coreference Resolution-p21
C语言-程序的编译
面试常问Future、FutureTask和CompletableFuture
[first song] deep learning of rebirth -keras (elementary)
[high concurrency] interviewer
C语言扫雷最新 递归展开 超详解(附源码)
编程学习记录--第2课【初识C语言】
编程学习记录——第4课【分支和循环语句】
人月神话阅读笔记
常见的SQL优化方法
百问网驱动大全学习(一)LCD驱动
Cmder的基础文件操作
发布 分辨率0.22m的建筑物分割数据库
[song] rebirth of me in py introductory training (7): function call
[song] rebirth of me in py introductory training (9): exception handling
【12】理解电路:从电报机到门电路,我们如何做到“千里传信”?
韦东山 数码相框 项目学习(二)在LCD上显示中文字符
Live Home 3D Pro interior home design tool
System Design的相关准备材料