0.6
This commit is contained in:
1 parent
dd82752869
commit
8d3d783d0a
2 files changed
+44
-4
No files matched your search
@@ -25,8 +25,7 @@ public class A123 {
|
|||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
int x=0;
|
int x=0;
|
||||||
int y=0;
|
/* HashMap<Integer,Integer> temp=new HashMap<>();
|
||||||
HashMap<Integer,Integer> temp=new HashMap<>();
|
|
||||||
for (int i = 0; i < prices.length; i++) {
|
for (int i = 0; i < prices.length; i++) {
|
||||||
for (int j = i; j < prices.length; j++) {
|
for (int j = i; j < prices.length; j++) {
|
||||||
if(temp.containsKey(prices[j]-prices[i])){
|
if(temp.containsKey(prices[j]-prices[i])){
|
||||||
@@ -40,8 +39,25 @@ public class A123 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
temp.get(x);
|
temp.get(x); */
|
||||||
return x+y;
|
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;i<prices.length;i++){
|
||||||
|
dp[4]=Math.max(dp[4],dp[3]+prices[i]);
|
||||||
|
dp[3]=Math.max(dp[3],dp[2]-prices[i]);
|
||||||
|
dp[2]=Math.max(dp[2],dp[1]+prices[i]);
|
||||||
|
dp[1]=Math.max(dp[1],-prices[i]);
|
||||||
|
}
|
||||||
|
for(int xx:dp){
|
||||||
|
if(xx>x){
|
||||||
|
x=xx;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dp[4];
|
||||||
}
|
}
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
A123 solution = new A123();
|
A123 solution = new A123();
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user