diff --git a/src/com/hong/www/A123.java b/src/com/hong/www/A123.java index 1afac1a..9eeb77f 100644 --- a/src/com/hong/www/A123.java +++ b/src/com/hong/www/A123.java @@ -25,8 +25,7 @@ public class A123 { * } */ int x=0; - int y=0; - HashMap temp=new HashMap<>(); + /* HashMap temp=new HashMap<>(); for (int i = 0; i < prices.length; i++) { for (int j = i; j < prices.length; j++) { if(temp.containsKey(prices[j]-prices[i])){ @@ -40,8 +39,25 @@ public class A123 { } } } - temp.get(x); - return x+y; + temp.get(x); */ + int[] dp=new int[5]; + dp[0]=0; + dp[1]=-Integer.MIN_VALUE; + dp[2]=0; + dp[3]=-Integer.MIN_VALUE; + dp[4]=0; + for(int i=0;ix){ + x=xx; + } + } + return dp[4]; } public static void main(String[] args) { A123 solution = new A123(); diff --git a/src/com/hong/www/A188.java b/src/com/hong/www/A188.java new file mode 100644 index 0000000..40a42f9 --- /dev/null +++ b/src/com/hong/www/A188.java @@ -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=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); + } +}