• + 0 comments
    public static int equalStacks(List<Integer> h1, List<Integer> h2, List<Integer> h3) {
        Stack <Integer> stack1 = getStackFromList(h1);
        Stack <Integer> stack2 = getStackFromList(h2);
        Stack <Integer> stack3 = getStackFromList(h3);
        int s1 = getStackSum(stack1);
        int s2 = getStackSum(stack2);
        int s3 = getStackSum(stack3);
        int min = Math.min(s1, Math.min(s2,s3));
        while ((!(s1 == s2 && s1 == s3)) && min>0 ) {
            if (s1 > min) {
                stack1.pop();
                s1 = getStackSum(stack1);
            }
            if (s2 > min) {
                stack2.pop();
                s2 = getStackSum(stack2);
            }
            if (s3 > min) {
                stack3.pop();
                s3 = getStackSum(stack3);
            }
            min = Math.min(s1, Math.min(s2,s3));
            
        }
        return min;
    
        }
        
        static int getStackSum(Stack<Integer> input) {
           return input.stream()
             .mapToInt(Integer::intValue)
             .sum();
        }
        
        static Stack<Integer> getStackFromList(List<Integer> input) {
            Stack <Integer> stack = new Stack<>();
           for (int i = input.size() - 1; i >=0; i--) {
               stack.push(input.get(i));
           }
           return stack;
        }