• + 0 comments

    JAVA

    public static int equalStacks(List<Integer> h1, List<Integer> h2, List<Integer> h3) {
            int s1 = h1.stream().mapToInt(x -> x.intValue()).sum();
            int s2 = h2.stream().mapToInt(x -> x.intValue()).sum();
            int s3 = h3.stream().mapToInt(x -> x.intValue()).sum();
            Collections.reverse(h1);
            Collections.reverse(h2);
            Collections.reverse(h3);
            while(s1 != s2 || s2 != s3){
                if(s1 > s2 || s1 > s3){
                    s1 -= h1.remove(h1.size() - 1);
                }
                
                if(s2 > s1 || s2 > s3){
                    s2 -= h2.remove(h2.size() - 1);
                }
                
                if(s3 > s2 || s3 > s1){
                    s3 -= h3.remove(h3.size() - 1);
                }
                
            }
            return s1;
        }