问题总览
| 类型 | 问题描述 | 完成 |
|---|---|---|
| 121. 买卖股票的最佳时机 | ✅ | |
| 122. 买卖股票的最佳时机 II | ✅ | |
| 123. 买卖股票的最佳时机 III | ✅ | |
| 188. 买卖股票的最佳时机 IV | ✅ | |
| 309. 最佳买卖股票时机含冷冻期 | ✅ | |
| 714. 买卖股票的最佳时机含手续费 | ✅ |
题解
相同题目:剑指 Offer 63. 股票的最大利润
在整个区间内只能买卖一次。
class Solution {
public int maxProfit(int[] prices) {
//dp[i][k] 第i天后的最大收益
//1表示持有
//0表示不持有
int len = prices.length;
if (len == 1) {
return 0;
}
int[][] dp = new int[len][2];
dp[0][0] = 0;
dp[0][1] = -prices[0];
for (int i = 1; i < len; i++) {
// 分为前一天持有和不持有两种情况
dp[i][0] = Math.max(dp[i - 1][1] + prices[i], dp[i - 1][0]);
// 分为前一天持有和不持有两种情况,因为只支持买卖一次,所以今天持有,收益就是-price[i]
dp[i][1] = Math.max(dp[i - 1][1], -prices[i]);
}
return dp[len - 1][0];
}
}
在整个区间内可以无限次的买卖,但同时持有的只能有一支股票。
121和122的区别就在于,支持多次买卖时dp[i-1][0]是有意义的。
class Solution {
public int maxProfit(int[] prices) {
//dp[i][k] 第i天后的最大收益
//1表示持有
//0表示不持有
int len = prices.length;
if (len == 1) {
return 0;
}
int[][] dp = new int[len][2];
dp[0][0] = 0;
dp[0][1] = -prices[0];
for (int i = 1; i < len; i++) {
// 分为前一天持有和不持有两种情况
dp[i][0] = Math.max(dp[i - 1][1] + prices[i], dp[i - 1][0]);
// 分为前一天持有和不持有两种情况
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
}
return dp[len - 1][0];
}
}
在整个区间内只可以买卖两次。
class Solution {
public int maxProfit(int[] prices) {
int n = prices.length;
int[][][] dp = new int[n][3][2];
for (int i = 0; i < n; i++) {
for (int k = 2; k >= 1; k--) {
if (i - 1 == -1) {
// 处理 base case
dp[i][k][0] = 0;
dp[i][k][1] = -prices[i];
continue;
}
dp[i][k][0] = Math.max(dp[i-1][k][0], dp[i-1][k][1] + prices[i]);
dp[i][k][1] = Math.max(dp[i-1][k][1], dp[i-1][k-1][0] - prices[i]);
}
}
// 穷举了 n × max_k × 2 个状态,正确。
return dp[n - 1][2][0];
}
}
在整个区间内可以买卖K次,和121、123类似。
class Solution {
public int maxProfit(int k, int[] prices) {
int n = prices.length;
int[][][] dp = new int[n][k + 1][2];
for (int i = 0; i < n; i++) {
for (int j = k; j >= 1; j--) {
if (i == 0) {
dp[i][j][0] = 0;
dp[i][j][1] = -prices[0];
continue;
}
dp[i][j][0] = Math.max(dp[i - 1][j][0], dp[i - 1][j][1] + prices[i]);
dp[i][j][1] = Math.max(dp[i - 1][j][1], dp[i - 1][j - 1][0] - prices[i]);
}
}
return dp[n - 1][k][0];
}
}
冷冻期带来的影响就是我们不能在卖完之后的第二天就买入 所以要在i-2天卖完后,才能在i天买入。
class Solution {
public int maxProfit(int[] prices) {
// dp[i][k] 前i天买卖后的最大收益
int len = prices.length;
if (len <= 1) {
return 0;
}
int[][] dp = new int[len][2];
dp[0][0] = 0;
dp[0][1] = -prices[0];
dp[1][0] = Math.max(0, prices[1] - prices[0]);
dp[1][1] = Math.max(-prices[0], -prices[1]);
for (int i = 2; i < len; i++) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 2][0] - prices[i]);
}
return dp[len - 1][0];
}
}
class Solution {
public int maxProfit(int[] prices, int fee) {
// dp[i][k] 前i天买卖后的最大收益
int len = prices.length;
int[][] dp = new int[len][2];
dp[0][0] = 0;
// 计算收益的时候都要减去手续费
dp[0][1] = -prices[0] - fee;
for (int i = 1; i < len; i++) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i] - fee);
}
return dp[len - 1][0];
}
}