当前位置:网站首页>力扣题解 动态规划(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)
边栏推荐
- 关于druid连接不上数据库的问题
- [song] rebirth of me in py introduction training (5): List
- Unity Shader 概述
- arcgis style样式表文件转换成geoserver sld文件
- Weidongshan digital photo frame project learning (III) transplantation of freetype
- C语言扫雷最新 递归展开 超详解(附源码)
- 编程学习记录——第9课【操作符】
- Lightroom classic 2022 v11.4 Chinese version "latest resources"
- [first song] rebirth of me in py introductory training (1)
- 使用-Wall清除代码隐患
猜你喜欢
![[first song] machine learning of rebirth - linear regression](/img/70/3efd9eacf88f55022eb52d096926f7.png)
[first song] machine learning of rebirth - linear regression

Weidongshan digital photo frame project learning (IV) simple TXT document display (e-paper book)

【头歌】重生之机器学习-线性回归

geonode geoserver win10 安装教程(亲测)

【Arduino】重生之Arduino 学僧(1)

根据SQL必知必会学习SQL(MYSQL)

面试常问Future、FutureTask和CompletableFuture

Leetcode每日一题30. 串联所有单词的子串
acwing每日一题 正方形数组的数目

【12】理解电路:从电报机到门电路,我们如何做到“千里传信”?
随机推荐
韦东山 数码相框 项目学习(三)freetype的移植
【头歌】重生之我在py入门实训中(12):Matplotlib接口和常用图形
安全帽反光衣检测识别数据集和yolov5模型
C# Json编码在TCP通讯中的一些使用总结
人月神话阅读笔记
acwing每日一题 正方形数组的数目
Dpdk network protocol stack VPP OVS DDoS Sdn nfv virtualization high performance expert Road
常见的SQL优化方法
安装windows下的redis
编程学习记录——第7课【函数】
浅记一下十大排序
力扣题解 二叉树(6)
malloc和new之间的不同-实战篇
Weidongshan digital photo frame project learning (I) display ASCII characters on LCD
开源WebGIS-相关知识
C语言-程序的编译
编程学习记录——第3课【初识C语言】
Baiwen driving Daquan learning (II) I2C driving
AE 3D particle system plug-in: Trapcode particle
百问网驱动大全学习(二)I2C驱动