感觉都上hard了
class Solution {
public int maxProfit(int[] prices) {
int[][] dp = new int[prices.length + 1][4];
dp[0][0] = -prices[0];
for(int i = 1; i <= prices.length;i++){
int price = prices[i - 1];
dp[i][0] = Math.max(dp[i - 1][0],Math.max(dp[i - 1][1] - price,dp[i - 1][3] - price));
dp[i][1] = Math.max(dp[i - 1][1],dp[i - 1][3]);
dp[i][2] = dp[i - 1][0] + price;
dp[i][3] = dp[i - 1][2];
}
return Math.max(dp[prices.length][1],Math.max(dp[prices.length][2],dp[prices.length][3]));
}
}
- 状态一:买入股票状态(今天买入股票,或者是之前就买入了股票然后没有操作)
- 卖出股票状态,这里就有两种卖出股票状态
- 状态二:两天前就卖出了股票,度过了冷冻期,一直没操作,今天保持卖出股票状态
- 状态三:今天卖出了股票
- 状态四:今天为冷冻期状态,但冷冻期状态不可持续,只有一天!