You are viewing a single comment's thread. Return to all comments →
Here is java solution. All test cases passed:)
static int equalStacks(int[] h1, int[] h2, int[] h3) { Stack<Integer> st1 = new Stack();st1.push(0); Stack<Integer> st2 = new Stack();st2.push(0); Stack<Integer> st3 = new Stack();st3.push(0); int sum1=0, sum2=0, sum3=0; for(int i=h1.length-1;i>=0;i--){ sum1 += h1[i]; st1.push(sum1); } for(int i=h2.length-1;i>=0;i--){ sum2 += h2[i]; st2.push(sum2); } for(int i=h3.length-1;i>=0;i--){ sum3 += h3[i]; st3.push(sum3); } int v1 =st1.peek(),v2 =st2.peek(), v3=st3.peek(); //we should avoid doing push and pop operation multiple time while(!(v1==v2 &&v1==v3)){ if(v1>=v2 && v1>=v3) st1.pop(); else if(v2>=v3 && v2>=v1) st2.pop(); else st3.pop(); v1 =st1.peek();v2 =st2.peek();v3=st3.peek(); } return st1.pop(); }
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 →
Here is java solution. All test cases passed:)