This commit is contained in:
Mrhongxing committed 2026-10-14 09:58:01 +08:00
1 parent dd82752869
commit 8d3d783d0a
2 files changed
+44 -4

No files matched your search

+24
View File
@@ -0,0 +1,24 @@
package com.hong.www;
public class A188 {
public int maxProfit(int k, int[] prices) {
int[] dp=new int[2*k+1];
for(int i=1;i<dp.length;i=i+2){
dp[i]=Integer.MIN_VALUE;
}
for(int price:prices){
for(int i=dp.length-1;i>=1;i=i-2){
dp[i]=Math.max(dp[i],dp[i-1]+price);
dp[i-1]=Math.max(dp[i-1],dp[i-2]-price);
}
}
return dp[dp.length-1];
}
public static void main(String[] args) {
A188 a188 = new A188();
int k = 2;
int[] prices = {3,2,6,5,0,3};
int result = a188.maxProfit(k, prices);
System.out.println(result);
}
}