We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
publicstaticintequalStacks(List<Integer>h1,List<Integer>h2,List<Integer>h3){// Write your code herelongsum1=h1.stream().mapToInt(Integer::intValue).sum();longsum2=h2.stream().mapToInt(Integer::intValue).sum();longsum3=h3.stream().mapToInt(Integer::intValue).sum();Collections.reverse(h1);Collections.reverse(h2);Collections.reverse(h3);Stack<Integer>s1=newStack<>();s1.addAll(h1);Stack<Integer>s2=newStack<>();s2.addAll(h2);Stack<Integer>s3=newStack<>();s3.addAll(h3);longminHeight;while(true){if(s1.isEmpty()||s2.isEmpty()||s3.isEmpty()){return0;}minHeight=Math.min(sum1,Math.min(sum2,sum3));while(sum1>minHeight){sum1-=s1.pop();}while(sum2>minHeight){sum2-=s2.pop();}while(sum3>minHeight){sum3-=s3.pop();}if(sum1==sum2&&sum2==sum3){return(int)sum1;}}}
Cookie support is required to access HackerRank
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 →
In JAVA8 :