309.最佳买卖股票时机含冷冻期
714.买卖股票的最佳时机含手续费
309.最佳买卖股票时机含冷冻期
class Solution {public int maxProfit(int[] prices) {//dp[i][j],第i天状态为j,所剩的最多现金为dp[i][j]/**dp[i][0] 持有股票状态dp[i][1] 保持保持卖出股票状态dp[i][2] 具体卖出股票状态dp[i][3] 冷冻期*//**dp[i][0] = dp[i-1][0]; dp[i-1][3] - price[i]; dp[i-1][1] - prices[i]dp[i][1] = dp[i-1][1]; dp[i-1][3]dp[i][2] = dp[i-1][0] + price[i]dp[i][3] = dp[i-1][2]*/int len = prices.length;if(len == 0) return 0;int[][] dp = new int[len][4];dp[0][0] = -prices[0];for(int i = 1;i<len;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[len-1][1],Math.max(dp[len-1][2],dp[len-1][3]));}
}
714.买卖股票的最佳时机含手续费
class Solution {public int maxProfit(int[] prices, int fee) {//dp[i][j],第i天状态为j,所剩的最多现金为dp[i][j]/**dp[i][0] 持有股票状态dp[i][0] = dp[i-1][0],dp[i-1][1] -price[i] dp[i][1] 不持有股票状态dp[i][1] = dp[i-1][1],dp[i-1][0] + price[i] -2*/int len = prices.length;if(len == 0) return 0;int[][] dp = new int[len][2];dp[0][0] = -prices[0];for(int i = 1;i<len;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 Math.max(dp[len-1][0],dp[len-1][1]);}
}