121. 买卖股票的最佳时机. - 力扣(LeetCode)
一次买卖股票的最大收益
1. dp[i][0] 表示第i天持有股票所得最多现金 ,
dp[i][1] 表示第i天不持有股票所得最多现金
2. dp[i][0] = max(dp[i - 1][0], -prices[i]); 前一天持有的收益,与今天持有的max
dp[i][1] = max(dp[i - 1][1], prices[i] + dp[i - 1][0]); 前一天不持有的收益与今天卖出的max
3. dp[0][0] -= prices[0];
dp[0][1] = 0;
4. 从前向后遍历
class Solution {
public int maxProfit(int[] prices) {
int n = prices.length;
int[][] dp = new int[n][2];
dp[0][0] = -prices[0];
dp[0][1] = 0;
for(int i=1; i<n; 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[n-1][1];
}
}
122.买卖股票的最佳时机II. - 力扣(LeetCode)
多次买卖股票的最大收益
递推公式发生了变化,如下,只有dp[i][0]不同
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
class Solution {
public int maxProfit(int[] prices) {
int n = prices.length;
int[][] dp = new int[n][2];
dp[0][0]=-prices[0];
dp[0][1] =0;
for(int i=1; i<n; 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[n-1][1];
}
}
123.买卖股票的最佳时机III. - 力扣(LeetCode)
至多两次买卖股票的最大收益
1. dp[i][j]中 i表示第i天,j为 [1 - 4] 四个状态,dp[i][j]表示第i天状态j所剩最大现金。
- 第一次持有股票
- 第一次不持有股票
- 第二次持有股票
- 第二次不持有股票
2. dp[i][1] = max(dp[i-1][0] - prices[i], dp[i - 1][1]);
dp[i][2] = max(dp[i - 1][1] + prices[i], 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]);
3. dp[0][1] = -prices[0];
dp[0][2] = 0;
dp[0][3] = -prices[0];
dp[0][4] = 0;
4. 从前向后遍历
class Solution {
public int maxProfit(int[] prices) {
int n = prices.length;
int[][] dp = new int[n][5];
dp[0][1]=-prices[0];
dp[0][2]=0;
dp[0][3]=-prices[0];
dp[0][4]=0;
for(int i=1; i<n; i++){
dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0]-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[n-1][4];
}
}
188.买卖股票的最佳时机IV. - 力扣(LeetCode)
至多K次买卖股票的最大收益
这里的规律就是状态[1~2K],奇数就是买入(持有),偶数就是卖出(不持有)
class Solution {
public int maxProfit(int k, int[] prices) {
int n = prices.length;
int[][] dp=new int[n][2*k+1];
for(int i=1; i<2*k; i+=2){
dp[0][i]=-prices[0];
}
for(int i=1; i<n; i++){
for(int j=0; j<2*k-1; j+=2){
dp[i][j+1] = Math.max(dp[i-1][j+1],dp[i-1][j]-prices[i]);
dp[i][j+2] = Math.max(dp[i-1][j+2],dp[i-1][j+1]+prices[i]);
}
}
return dp[n-1][k*2];
}
}
309.最佳买卖股票时机含冷冻期. - 力扣(LeetCode)
- 0:持有股票状态(今天买入股票,或者是之前就买入了股票然后没有操作,一直持有dp[i][0] = max(dp[i - 1][0], dp[i - 1][3] - prices[i], dp[i - 1][1] - prices[i]);
- 不持有股票状态,这里就有两种卖出股票状态
- 1:保持卖出股票的状态(两天前就卖出了股票,度过一天冷冻期。或者是前一天就是卖出股票状态,一直没操作) dp[i][1] = max(dp[i - 1][1], dp[i - 1][3]);
- 2:今天卖出股票 dp[i][2] = dp[i - 1][0] + prices[i];
- 3:今天为冷冻期状态 dp[i][3] = dp[i - 1][2];
单独列出一个状态的归类为「不持有股票的状态」,因为本题有冷冻期,而冷冻期的前一天,只能是 「今天卖出股票」状态。如果是 「不持有股票状态」那么就很模糊,因为不一定是 卖出股票的操作。
dp[0][0] = -prices[0]; dp[0][1]=0; dp[0][2]=0; dp[0][3]=0
从前向后遍历
class Solution {
public int maxProfit(int[] prices) {
int n = prices.length;
int[][] dp = new int[n][4];
dp[0][0] = -prices[0];
for(int i=1; i<n; i++){
dp[i][0] = Math.max(dp[i-1][0], Math.max(dp[i-1][3]-prices[i],dp[i-1][1]-prices[i]));
dp[i][1] = Math.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 Math.max(dp[n-1][1], Math.max(dp[n-1][2],dp[n-1][3]));
}
}
714.买卖股票的最佳时机含手续费
- dp[i][0] 表示第i天持有股票所得最多现金。dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
- dp[i][1] 表示第i天不持有股票所得最多现金。dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i] - fee);
class Solution {
public int maxProfit(int[] prices, int fee) {
int n = prices.length;
int[][] dp = new int[n][2];
dp[0][0]=-prices[0];
for(int i=1; i<n; 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[n-1][1];
}
}