33,028
社区成员
发帖
与我相关
我的任务
分享
public class EqualTowers {
/**
* @param args the command line arguments
*/
private static final int MAX=250000;
private int[][] mem= new int[50][MAX+1];
private int[] bricks;
public int height(int[] bricks){
this.bricks=bricks;
return go(0,0);
}
public int go(int k,int t){
if(t>MAX){
return -1;
}
if(k==bricks.length)
{
return ((t==0)?0:-1);
}
if(mem[k][t]==0){
//add to the First tower
int d1=go(k+1,t+bricks[k]);
//do nothing;
int d2=go(k+1,t);
//add to the second tower
int d3=go(k+1,Math.abs(bricks[k]-t));
if(d3!=-1){
d3+=Math.min(t,bricks[k]);
}
//pick the best result
mem[k][t]=Math.max(d1,Math.max(d2, d3));
//Prevent a 0-size tower in the final result;
if(t==0&&k==0&&mem[k][t]==0){
mem[k][t]=-1;
}
mem[k][t]+=2;
}
return mem[k][t]-2;
}
public static void main(String[] args) {
// TODO code application logic here
}
}