You are viewing a single comment's thread. Return to all comments →
public static int highest(int s1, int s2, int s3){ return Math.max(s1,Math.max(s2,s3)); }
public static int equalStacks(List<Integer> h1, List<Integer> h2, List<Integer> h3) { // Write your code here int n1=h1.size(); int n2=h2.size(); int n3=h3.size(); int s1=0; int s2=0; int s3=0; int high; int p1=0; int p2=0; int p3=0; for(int i=0;i<n1;i++){ s1+=h1.get(i); } for(int i=0;i<n2;i++){ s2+=h2.get(i); } for(int i=0;i<n3;i++){ s3+=h3.get(i); } while(p1<h1.size() && p2<h2.size() && p3<h3.size()){ if(s1==s2 && s2==s3){ return s1; } else{ high=highest(s1,s2,s3); if(high==s1){ s1-=h1.get(p1); p1++; } if(high==s2){ s2-=h2.get(p2); p2++; } if(high==s3){ s3-=h3.get(p3); p3++; } } } return 0;
}
Seems like cookies are disabled on this browser, please enable them to open this website
Equal Stacks
You are viewing a single comment's thread. Return to all comments →
public static int highest(int s1, int s2, int s3){ return Math.max(s1,Math.max(s2,s3)); }
}
}