121. 买卖股票的最佳时机
题目链接:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/
文档讲解:https://programmercarl.com/0121.%E4%B9%B0%E5%8D%96%E8%82%A1%E7%A5%A8%E7%9A%84%E6%9C%80%E4…
视频讲解:https://www.bilibili.com/video/BV1Xe4y1u77q
思路
- 确定dp数组以及下标的含义:
dp[i][0]
表示第i天持有股票的最大金额,dp[i][1]
表示第i天不持有股票的最大金额。 - 确定递推公式:
dp[i][0]
有两种情况组成,一种是前一天就持有股票,一种是当天才买入股票,取最大的:dp[i][0] = Math.max(dp[i - 1][0], -prices[i])
;dp[i][1]
有两种情况组成,一种是前一天不持有股票,一种是当天卖掉股票,取最大的:dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i])
。 - dp数组如何初始化:
dp[0][0] = - prices[0];
,dp[0][1] = 0;
- 确定遍历顺序:
- 打印dp数组,用于debug
代码
class Solution {
public int maxProfit(int[] prices) {
int dp[][] = new int[prices.length][2];
dp[0][0] = - prices[0];
dp[0][1] = 0;
for (int i = 1; i < prices.length; i++){
dp[i][0] = Math.max(dp[i - 1][0], - prices[i]);
dp[i][1] = Math.max(dp[i - 1][1], prices[i] + dp[i - 1][0]);
}
return dp[len - 1][1];
}
}
分析:时间复杂度:O(n),空间复杂度:O(n)。
122.买卖股票的最佳时机II
题目链接:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/
文档讲解:https://programmercarl.com/0122.%E4%B9%B0%E5%8D%96%E8%82%A1%E7%A5%A8%E7%9A%84%E6%9C%80%E4…
视频讲解:https://www.bilibili.com/video/BV1D24y1Q7Ls
思路
代码中和买卖股票1的区别在于dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
,因为本题是可以多次买卖股票,所以在当天买股票的时候可能之前已经买卖过,已经是有收益的了。
代码
class Solution {
public int maxProfit(int[] prices) {
int[][] dp = new int[prices.length][2];
dp[0][0] -= prices[0]; // dp[i][0]表示持有当天股票
dp[0][1] = 0; // dp[i][1]表示不持有当天股票
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 Math.max(dp[prices.length - 1][1], dp[prices.length - 1][0]);
}
}
分析:时间复杂度:O(n),空间复杂度:O(n)。
123.买卖股票的最佳时机III
题目链接:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-iii/
文档讲解:https://programmercarl.com/0123.%E4%B9%B0%E5%8D%96%E8%82%A1%E7%A5%A8%E7%9A%84%E6%9C%80%E4…
视频讲解:https://www.bilibili.com/video/BV1WG411K7AR
思路
- 确定dp数组以及下标的含义:第i天一共有五个状态
dp[i][0]
:没有操作 (也可以不设置这个状态,没有操作金额就是0)dp[i][1]
:第一次持有股票dp[i][2]
:第一次不持有股票dp[i][3]
:第二次持有股票dp[i][4]
:第二次不持有股票
- 确定递推公式:
- 达到
dp[i][1]
状态,有两个具体操作,选最大的:
操作一:第i天买入股票了,那么dp[i][1] = dp[i-1][0] - prices[i]
操作二:第i天没有操作,而是沿用前一天买入的状态,即dp[i][1] = dp[i - 1][1]
- 达到
dp[i][2]
状态也有两个操作:
操作一:第i天卖出股票了,那么dp[i][2] = dp[i - 1][1] + prices[i]
操作二:第i天没有操作,沿用前一天卖出股票的状态,即dp[i][2] = dp[i - 1][2]
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]);
- 达到
- dp数组如何初始化:由于一天内可以买卖一次也可以买卖两次,所以
dp[0][1] = -prices[0];
,dp[0][3] = -prices[0];
,而卖出的就是0。 - 确定遍历顺序:从第二天开始正序遍历。
- 打印dp数组,用于debug
代码
class Solution {
public int maxProfit(int[] prices) {
int[][] dp = new int[prices.length][5]; // 0: 没有操作, 1: 第一次买入, 2: 第一次卖出, 3: 第二次买入, 4: 第二次卖出
dp[0][1] = -prices[0];
dp[0][3] = -prices[0];
for (int i = 1; i < prices.length; i++) {
dp[i][1] = Math.max(dp[i - 1][1], -prices[i]);
dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1] + prices[i]);
dp[i][3] = Math.max(dp[i - 1][3], dp[i - 1][2] - prices[i]);
dp[i][4] = Math.max(dp[i - 1][4], dp[i - 1][3] + prices[i]);
}
return dp[prices.length - 1][4];
}
}
分析:时间复杂度:O(n),空间复杂度:O(5n)。