Home > AI > Data Structure > Stack >

(HackerRank) Equal Stacks

https://www.hackerrank.com/challenges/equal-stacks/problem

Solution (Java)


import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'equalStacks' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts following parameters:
     *  1. INTEGER_ARRAY h1
     *  2. INTEGER_ARRAY h2
     *  3. INTEGER_ARRAY h3
     */

    public static int equalStacks(List<Integer> h1, List<Integer> h2, List<Integer> h3) {
        // Write your code here

        // 1 make Stack
        Stack<Integer> sh1 = new Stack();
        Stack<Integer> sh2 = new Stack();
        Stack<Integer> sh3 = new Stack();

        int h1Height = 0, h2Height = 0, h3Height = 0;

        int mh = Math.max(h1.size(), h2.size());
        mh = Math.max(mh, h3.size());

        for (int i=0; i<mh; i++) {

            if (i < h1.size()) {
                h1Height += h1.get(h1.size()-1-i);
                sh1.push(h1Height);
            }

            if (i < h2.size()) {
                h2Height += h2.get(h2.size()-1-i);
                sh2.push(h2Height);
            }

            if (i < h3.size()) {
                h3Height += h3.get(h3.size()-1-i);
                sh3.push(h3Height);
            }
        }

        // 2 remove
        while (true) {
            if (sh1.isEmpty() || sh2.isEmpty() || sh3.isEmpty()) {
                return 0;
            }

            if (h1Height == h2Height && h1Height == h3Height) {
                return h1Height;
            }

            h1Height = sh1.peek();
            h2Height = sh2.peek();
            h3Height = sh3.peek();


            if (h1Height >= h2Height && h1Height >= h3Height) {
                sh1.pop();
            } else if (h2Height >= h1Height && h2Height >= h3Height) {
                sh2.pop();
            } else if (h3Height >= h1Height && h3Height >= h2Height) {
                sh3.pop();
            }


        }
    }
}



public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("good"));

        String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");

        int n1 = Integer.parseInt(firstMultipleInput[0]);

        int n2 = Integer.parseInt(firstMultipleInput[1]);

        int n3 = Integer.parseInt(firstMultipleInput[2]);

        List<Integer> h1 = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
                .map(Integer::parseInt)
                .collect(toList());

        List<Integer> h2 = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
                .map(Integer::parseInt)
                .collect(toList());

        List<Integer> h3 = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
                .map(Integer::parseInt)
                .collect(toList());

        int result = Result.equalStacks(h1, h2, h3);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();

        bufferedReader.close();
        bufferedWriter.close();
    }
}

Leave a Reply