Hackerrank - Cavity

 You are given a square map as a matrix of integer strings. Each cell of the map has a value denoting its depth. We will call a cell of the map a cavity if and only if this cell is not on the border of the map and each cell adjacent to it has strictly smaller depth. Two cells are adjacent if they have a common side, or edge.

Find all the cavities on the map and replace their depths with the uppercase character X.

Example

The grid is rearranged for clarity:

989
191
111

Return:

989
1X1
111

The center cell was deeper than those on its edges: [8,1,1,1]. The deep cells in the top two corners do not share an edge with the center cell, and none of the border cells is eligible.

Function Description

Complete the cavityMap function in the editor below.

cavityMap has the following parameter(s):

  • string grid[n]: each string represents a row of the grid

Returns

  • string{n}: the modified grid

Input Format

The first line contains an integer , the number of rows and columns in the grid.

Each of the following  lines (rows) contains  positive digits without spaces (columns) that represent the depth at .

Constraints

Sample Input

STDIN   Function
-----   --------
4       grid[] size n = 4
1112    grid = ['1112', '1912', '1892', '1234']
1912
1892
1234

Sample Output

1112
1X12
18X2
1234

Explanation

The two cells with the depth of 9 are not on the border and are surrounded on all sides by shallower cells. Their values are replaced by X.





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 {


 public static List<String> cavityMap(List<String> grid) {
        int n = grid.size();
        ArrayList<ArrayList<Integer>> matrix = new ArrayList<ArrayList<Integer>>(); 

        for (int i = 0; i < n; i++) {
            ArrayList<Integer> row = new ArrayList<>();
            for (int j = 0; j < n; j++) {
               row.add(Integer.parseInt(grid.get(i).charAt(j)+""));                
            }
            matrix.add(row);
        }

        for (int i = 1; i < n-1; i++) {
            ArrayList<Integer> row = matrix.get(i);
            for (int j = 1; j < n-1; j++) {
                ArrayList<Integer> edges = new ArrayList<>();
                
                edges.add(matrix.get(i).get(j-1));
                edges.add(matrix.get(i).get(j+1));
                edges.add(matrix.get(i-1).get(j));
                edges.add(matrix.get(i+1).get(j));


                // System.out.println(edges);
              

                int currentNum = row.get(j);
                // edges.add(currentNum);
                // System.out.println(currentNum);
                if (currentNum > Collections.max(edges) && !(Collections.frequency(edges, currentNum) > 1) && !edges.contains(Integer.MIN_VALUE)) {
                    currentNum = Integer.MIN_VALUE;
                }

                row.set(j, currentNum);
                matrix.set(i, row);
            }

            
        }

        for (int i = 0; i < n; i++) {
            String row = "";
            for (int j = 0; j < n; j++) {
               int currentElement = matrix.get(i).get(j);
               row+= currentElement == Integer.MIN_VALUE? "X": currentElement+"";                
            }
            grid.set(i, row);
        }

        

        return grid;

    }


}

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(System.getenv("OUTPUT_PATH")));

        int n = Integer.parseInt(bufferedReader.readLine().trim());

        List<String> grid = IntStream.range(0, n).mapToObj(i -> {
            try {
                return bufferedReader.readLine();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        })
            .collect(toList());

        List<String> result = Result.cavityMap(grid);

        bufferedWriter.write(
            result.stream()
                .collect(joining("\n"))
            + "\n"
        );

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

Comments

Popular posts from this blog

Hackerrank - Quicksort 2 - Sorting

Hackerrank - Day of the Programmer

Hackerrank - UNIQUE ARMSTRONG NUMBER