25 lines
687 B
Java
25 lines
687 B
Java
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);
|
|
}
|
|
}
|