题目1:【二刷完成】
class Solution {
public int maxProfit(int[] prices) {
int[][] dp = new int[prices.length][5];
//不操作,第一次持有,第一次不持有,第二次持有,第二次不持有
dp[0][0]= 0 ;
dp[0][1]=-prices[0];
dp[0][2]=0;
dp[0][3]=-prices[0];
dp[0][4]=0;
for (int i=1;i<prices.length;i++){
dp[i][0]=dp[i-1][0];
dp[i][1]=Math.max(dp[i-1][1],dp[i][0]-prices[i]);
dp[i][2]=Math.max(dp[i-1][2],dp[i][1]+prices[i]);
dp[i][3] = Math.max(dp[i - 1][3], dp[i - 1][2] - prices[i]);
dp[i][4] = Math.max(dp[i - 1][4], dp[i - 1][3] + prices[i]);
}
return dp[prices.length-1][4];
}
}
题目2:【二刷完成】
class Solution {
public int maxProfit(int k, int[] prices) {
int[][] dp = new int[prices.length][2*k+1];
for (int i=0;i<2*k+1;i++){
if (i%2==1){
dp[0][i]=-prices[0];
}
}
for(int i=1;i<prices.length;i++){
for (int j=1;j<2*k+1;j++){
if (j%2==1){
dp[i][j]=Math.max(dp[i-1][j],dp[i-1][j-1]-prices[i]);
}else{
dp[i][j]=Math.max(dp[i-1][j],dp[i-1][j-1]+prices[i]);
}
}
}
return dp[prices.length-1][2*k];
}
}