1、附上题目链接
121. 买卖股票的最佳时机
122. 买卖股票的最佳时机 II
123. 买卖股票的最佳时机 III
188. 买卖股票的最佳时机 IV
309. 最佳买卖股票时机含冷冻期
714. 买卖股票的最佳时机含手续费
2、代码关键点详解
2.1 关键点:买卖股票的最佳时机
2.1.1 买卖股票的最佳时机
- 给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。
class Solution {
public int maxProfit(int[] prices) {
/* 动态规划 */
// if (prices == null || prices.length == 0) return 0;
// int[][] dp = new int[prices.length][2];
// dp[0][0] = 0;
// dp[0][1] = -prices[0];
// for (int i = 1; i < prices.length; 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], - prices[i]);
// }
// return dp[prices.length - 1][0];
/* 动态规划 2 */
if (prices.length < 2) return 0;
int minPrice = Integer.MAX_VALUE;
int res = 0;
for (int i = 0; i < prices.length; i++) {
if (prices[i] < minPrice) {
minPrice = prices[i];
continue;
}
res = Math.max(res, prices[i] - minPrice);
}
return res;
}
}
关键点1:买卖一次
- 记录今天之前的股票最低价
minPrice = prices[i]; //更新最低价格
,以及计算如今天卖出的话,最大收入多少res = Math.max(res, prices[i] - minPrice); //更新最大收益
2.1.2 买卖股票的最佳时机 II
- 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
class Solution {
public int maxProfit(int[] prices) {
// 贪心算法
// int res = 0;
// for (int i = 0; i < prices.length - 1; i++) {
// if (prices[i + 1] - prices[i] > 0) {
// res += prices[i + 1] - prices[i];
// }
// }
// return res;
/* 动态规划 */
int[][] dp = new int[prices.length][2];
dp[0][0] = 0;
dp[0][1] = -prices[0];
for (int i = 1; i < prices.length; 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]);
}
return dp[prices.length - 1][0];
}
}
关键点1:买卖多次
- 交易多次时,第i天交易完成后持股的最大利润 与 第i - 1天交易完成后持股的最大利润 和 第i - 1天交易完成后没有持股的最大利润均有关
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
2.1.3 买卖股票的最佳时机 III
- 给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。
class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) return 0;
int buy1 = -prices[0], sell1 = 0;
int buy2 = -prices[0], sell2 = 0;
int res = 0;
for (int i = 1; i < prices.length; i++ ) {
buy1 = Math.max(buy1, -prices[i]);
sell1 = Math.max(sell1, buy1 + prices[i]);
buy2 = Math.max(buy2, sell1 - prices[i]);
sell2 = Math.max(sell2, buy2 + prices[i]);
res = Math.max(res, sell2);
}
return res;
}
}
关键点1:买卖两次
- 第二买和第一次买的收入有关
buy2 = Math.max(buy2, sell1 - prices[i]);
2.1.1 买卖股票的最佳时机 IV
- 给定一个整数数组 prices ,它的第 i 个元素 prices[i] 是一支给定的股票在第 i 天的价格。设计一个算法来计算你所能获取的最大利润。你最多可以完成 k 笔交易。
class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) return 0;
int[][][] dp = new int[prices.length][k + 1][2];
for (int j = k; j > 0; j--) {
dp[0][j][0] = 0; //第0天,还有j次,手里没股票,最大利润为0
dp[0][j][1] = -prices[0]; //第0天,还有j次,手里有股票,因为还没有盈利,最大利润为 负prices[i]
}
int res = 0;
for (int i = 1; i < prices.length; i++ ) {
for (int j = k; j > 0; j--) {
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]);
res = Math.max(res, dp[i][j][0]);
}
}
return res;
}
}
关键点1:买卖k次
- 注意每买卖一次,需要记录一次交易次数
dp[i][j][1] = Math.max(dp[i-1][j][1], dp[i-1][j-1][0] - prices[i]);
2.1.5 最佳买卖股票时机含冷冻期
- 给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 。设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天)。
class Solution {
public int maxProfit(int[] prices) {
// if (prices == null || prices.length == 0) return 0; //边界条件
// int[][] dp = new int[prices.length][3];
// dp[0][0] = 0;
// dp[0][1] = -prices[0];
// dp[0][2] = 0;
// for (int i = 1; i < prices.length; i++) {
// // dp[i][0]: 手上不持有股票,并且今天不是由于卖出股票而不持股,我们拥有的现金数
// // dp[i][1]: 手上持有股票时,我们拥有的现金数
// // dp[i][2]: 手上不持有股票,并且今天是由于卖出股票而不持股,我们拥有的现金数
// dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][2]);
// dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
// dp[i][2] = dp[i - 1][1] + prices[i];
// }
// return Math.max(dp[prices.length - 1][0], dp[prices.length - 1][2]);
if (prices == null || prices.length == 0) return 0;
int[][] dp = new int[prices.length][3];
dp[0][0] = 0; // 卖出
dp[0][1] = -prices[0]; // 买入
dp[0][2] = 0; // 冷冻期
int res = 0;
for (int i = 1; i < prices.length; i++) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]); // dp[i - 1][1] + prices[i]:前一天买入 + 今天卖出
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][2] - prices[i]); // dp[i - 1][2] - prices[i]:前一天冷冻期 + 今天买入
dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][0]); // dp[i - 1][0]:昨天卖出,今天冷冻期
}
return dp[prices.length - 1][0];
}
}
关键点1:冷冻期
- 记住三种状态的状态转移
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]); // dp[i - 1][1] + prices[i]:前一天买入 + 今天卖出
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][2] - prices[i]); // dp[i - 1][2] - prices[i]:前一天冷冻期 + 今天买入
dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][0]); // dp[i - 1][0]:昨天卖出,今天冷冻期
2.1.6 买卖股票的最佳时机含手续费
- 给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 ;非负整数 fee 代表了交易股票的手续费用。你可以无限次地完成交易,但是你每笔交易都需要付手续费。如果你已经购买了一个股票,在卖出它之前你就不能再继续购买股票了。返回获得利润的最大值。注意:这里的一笔交易指买入持有并卖出股票的整个过程,每笔交易你只需要为支付一次手续费。
class Solution {
public int maxProfit(int[] prices, int fee) {
/* 动态规划 */
if (prices == null || prices.length == 0) return 0;
int[][] dp = new int[prices.length][2];
dp[0][0] = 0;
dp[0][1] = -prices[0] - fee;
for (int i = 1; i < prices.length; 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[prices.length - 1][0];
}
}
关键点1:含手续费
- 类似于2.1.2 题,在该题的基础上,增加了手续费