Sort by

recency

|

863 Discussions

|

  • + 0 comments

    C++

    vector<string> cavityMap(vector<string> &grid) {
        for (int i = 1; i < grid.size() - 1; i++) {
            for (int j = 1; j < grid[i].length() - 1; j++) {
                if (grid[i][j] > grid[i][j - 1] && grid[i][j] > grid[i][j + 1]) {
                    if(grid[i][j] > grid[i-1][j] && grid[i][j] > grid[i+1][j]) {
                        grid[i][j] = 'X';
                    }
                }
            }
        }
        return grid;
    }
    
  • + 0 comments

    import java.io.; import java.util.; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList;

    class Result {

    /*
     * Complete the 'cavityMap' function below.
     *
     * The function is expected to return a STRING_ARRAY.
     * The function accepts STRING_ARRAY grid as parameter.
     */
    
    public static List<String> cavityMap(List<String> grid) {
        int n = grid.size();
        List<String> result = new ArrayList<>(grid);
    
        for (int i = 1; i < n - 1; i++) {
            char[] row = grid.get(i).toCharArray();
            for (int j = 1; j < row.length - 1; j++) {
                int current = row[j] - '0';
                int top = grid.get(i - 1).charAt(j) - '0';
                int bottom = grid.get(i + 1).charAt(j) - '0';
                int left = row[j - 1] - '0';
                int right = row[j + 1] - '0';
    
                if (current > top && current > bottom && current > left && current > right) {
                    row[j] = 'X';
                }
            }
            result.set(i, new String(row));
        }
    
        return result;
    }
    

    }

    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();
    }
    

    }

  • + 0 comments
    def cavityMap(grid):
        # Write your code here
        n = len(grid)
        new_grid = grid.copy()
        for i in range(1, n-1):
            for j in range(1, n-1):
                current = grid[i][j]
                top = grid[i-1][j]
                bottom = grid[i+1][j]
                left = grid[i][j-1]
                right = grid[i][j+1]
                if current > top and current > bottom and current > left and current > right:
                    new_grid[i] = new_grid[i][:j] + "X" + new_grid[i][j+1:]
                    
        return new_grid
    
  • + 0 comments

    Here is problem solution in Python, java, C++, C and Javascript - https://programmingoneonone.com/hackerrank-cavity-map-problem-solution.html

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/BweZoGPs08M

    vector<string> cavityMap(vector<string> grid) {
        for(int i = 1; i < grid.size() - 1; i++){
            for(int j = 1; j < grid[i].size()-1; j++){
                if(grid[i][j] > grid[i][j-1] && grid[i][j] > grid[i][j+1])
                    if(grid[i][j] > grid[i-1][j] && grid[i][j] > grid[i+1][j])
                    grid[i][j] = 'X'; 
            }
        }
        return grid;
    }