
public int maxProfit(int[] prices) {
long[][] dp = new long[prices.length + 1][5];
for (long[] dayStatus : dp) {
Arrays.fill(dayStatus, Integer.MIN_VALUE);
}
dp[0][0] = 0; // 第零天的“没买过”状态的收入值是 0,其他都为 Integer.MIN_VALUE 表示没计算过
for (int i = 1; i < dp.length; i++) {
dp[i][0] = 0;
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i - 1]);
dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1] + prices[i - 1]);
dp[i][3] = Math.max(dp[i - 1][3], dp[i - 1][2] - prices[i - 1]);
dp[i][4] = Math.max(dp[i - 1][4], dp[i - 1][3] + prices[i - 1]);
}
int res = Integer.MIN_VALUE;
for (int i = 0; i < dp[prices.length].length; i++) {
res = Math.max(res, (int)dp[prices.length][i]);
}
return res;
}