总结
状态转移方程:
base case:
状态转移方程,就是调用之前的数据得出今天的数据,同时记录好今天的数据,等着将来的一天去调用。
其中最重要的两步是 状态转移方程 和 base case
动态规划 也像带表格的回溯和递归
对于二维数组构建的动态规划一般可以优化为 一维数组或者O(1)
121. 买卖股票的最佳时机
买卖一次
class Solution {
public int maxProfit(int[] prices) {
int n = prices.length;
// base case: dp[-1][0] = 0, dp[-1][1] = -infinity
int dp_i_0 = 0, dp_i_1 = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
// dp[i][0] = max(dp[i-1][0], dp[i-1][1] + prices[i])
dp_i_0 = Math.max(dp_i_0, prices[i] + dp_i_1); //prices[i] +(-买入的时候的价格);
// dp[i][1] = max(dp[i-1][1], -prices[i])
dp_i_1 = Math.max(dp_i_1, -prices[i]);
System.out.println(dp_i_0+" : "+dp_i_1);
}
return dp_i_0;
}
}
122. 买卖股票的最佳时机 II
不限次数
class Solution {
public int maxProfit(int[] prices) {
int n = prices.length;
int dp_i_0 = 0, dp_i_1 = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
int temp = dp_i_0;
dp_i_0 = Math.max(dp_i_0, dp_i_1 + prices[i]);
dp_i_1 = Math.max(dp_i_1, temp - prices[i]);
}
return dp_i_0;
}
}
123. 买卖股票的最佳时机 III
最多买卖两次
class Solution {
public int maxProfit(int[] prices) {
int dp_i10 = 0, dp_i20 = 0;
int dp_i11 = Integer.MIN_VALUE, dp_i21 = Integer.MIN_VALUE;
for(int price : prices) {
dp_i20 = Math.max(dp_i20, dp_i21 + price);
dp_i21 = Math.max(dp_i21, dp_i10 - price);
dp_i10 = Math.max(dp_i10, dp_i11 + price);
dp_i11 = Math.max(dp_i11, -price);
}
return dp_i20;
}
}
188. 买卖股票的最佳时机 IV
给定任意买卖次数
class Solution {
public int maxProfit(int k, int[] prices) {
int n = prices.length;
if (k > n/2)
return maxProfit_k_inf(prices);
int[][][] dp = new int[n][k + 1][2];
for (int i = 0; i < n; i++)
for (int j = k; j >= 1; j--) {
if (i - 1 == -1) {
dp[0][j][0] = 0;
dp[0][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];
}
private int maxProfit_k_inf(int[] prices) {
int dp_i_0 = 0, dp_i_1 = Integer.MIN_VALUE;
for (int i = 0; i < prices.length; i++) {
int temp = dp_i_0;
dp_i_0 = Math.max(dp_i_0, dp_i_1 + prices[i]);
dp_i_1 = Math.max(dp_i_1, temp - prices[i]);
}
return dp_i_0;
}
}
309. 最佳买卖股票时机含冷冻期
买卖次数不限,卖完股票需要隔一天才能买股票
class Solution {
public int maxProfit(int[] prices) {
int dp_i_0 = 0, dp_i_1 = Integer.MIN_VALUE;
int pre_i_0 = 0;
for(int i = 0; i < prices.length; i++) {
int temp = dp_i_0;
dp_i_0 = Math.max(dp_i_0, dp_i_1 + prices[i]);
dp_i_1 = Math.max(dp_i_1, pre_i_0 - prices[i]);
pre_i_0 = temp;
}
return dp_i_0;
}
}
714. 买卖股票的最佳时机含手续费
不限次数,交易含手续费
class Solution {
public int maxProfit(int[] prices, int fee) {
int dp_i_0 = 0, dp_i_1 = Integer.MIN_VALUE;
for(int price : prices) {
int temp = dp_i_0;
dp_i_0 = Math.max(dp_i_0, dp_i_1 + price);
dp_i_1 = Math.max(dp_i_1, temp - price - fee);
}
return dp_i_0;
}
}
原文链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/discuss/108870/Most-consistent-ways-of-dealing-with-the-series-of-stock-problems
中文链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/solution/yi-ge-fang-fa-tuan-mie-6-dao-gu-piao-wen-ti-by-lab/