代码随想录算法训练营天 第九章 五十一天| 309.最佳买卖股票时机含冷冻期 714.买卖股票的最佳时机含手续费
309.最佳买卖股票时机含冷冻期
class Solution {
public int maxProfit(int[] prices) {
int[][] dp = new int[prices.length][4];
dp[0][0] = -prices[0];
for (int i = 1; i < prices.length; i++) {
dp[i][0] = Math.max(Math.max(dp[i-1][0],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[prices.length-1][1],Math.max(dp[prices.length-1][2],dp[prices.length-1][3]));
}
}
714.买卖股票的最佳时机含手续费
class Solution {
public int maxProfit(int[] prices, int fee) {
int[][] dp = new int[prices.length][2];
dp[0][0] = -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] -fee);
dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0]+prices[i]);
}
return dp[prices.length-1][1];
}
}