Sort by

recency

|

850 Discussions

|

  • + 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;
    }
    
  • + 0 comments

    Java clean solutaion

    public static List<Character> getAdjacent(List<String> grid, int i,int j){
        return Arrays.asList(grid.get(i+1).charAt(j), grid.get(i-1).charAt(j),
                grid.get(i).charAt(j+1), grid.get(i).charAt(j-1));
        }
        
        public static int getInt(char c){
            return c - '0';
            }
           
        public static List<String> cavityMap(List<String> grid) {
        int n = grid.size();
        List<String> new_grid = new ArrayList<>();
        
        for(int i = 0; i < n; i++){
            String line = "";
            for(int j = 0; j < n; j++){
                char c = grid.get(i).charAt(j);
                if(i == 0 || i == n-1 || j == 0 || j == n-1){
                    line += c;
                    continue;
                    }
                boolean is_cavity = true;
                
                
                for(char adj: getAdjacent(grid, i, j)){
                    if(getInt(adj) >= getInt(c))
                      is_cavity = false; 
                    }
                if(is_cavity)
                    c = 'X';
                
                line += c;
                
                }
            new_grid.add(line);
            }
        
        return new_grid;
    
        }
    
  • + 0 comments

    SWIFT

    func cavityMap(grid: [String]) -> [String] {
        // Write your code here
        if grid.first!.count < 3 { return grid }
        
        var gridUTF8: [[UInt8]] = grid.map{ [UInt8]($0.utf8) }
        
        let gridMaxIndex: Int = gridUTF8.count - 1
        let gridContentMaxIndex: Int = gridUTF8.first!.count - 1
        
        for index1 in 1 ... gridMaxIndex - 1 {
            for index2 in 1 ... gridContentMaxIndex - 1 {
                let currentGrid: UInt8 = gridUTF8[index1][index2]
                let isHValid: Bool = gridUTF8[index1][index2 - 1] < currentGrid && gridUTF8[index1][index2 + 1] < currentGrid
                let isVValid: Bool = gridUTF8[index1 - 1][index2] < currentGrid && gridUTF8[index1 + 1][index2] < currentGrid
                if isHValid && isVValid  {
                    gridUTF8[index1][index2] = 88
                    continue
                }
            }
        }
        
        return gridUTF8.map{ String(bytes: $0, encoding: String.Encoding.utf8) ?? "" }
    }
    
  • + 0 comments
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    void cavityMap(int n,char** grid){
        for(int i=1;i<n-1;i++){
            for(int j=1;j<n-1;j++){
                if(grid[i][j]>grid[i][j-1]&&grid[i][j]>grid[i][j+1]&&grid[i][j]>grid[i-1][j]&&grid[i][j]>grid[i+1][j]){
                    grid[i][j]='X';
                }
            }
        }
        for(int i=0;i<n;i++){
            printf("%s\n", grid[i]);
        }
    }
    
    int main()
    {
        int n;
        scanf("%d",&n);
        char** grid=(char**)malloc(n*sizeof(char*));
        for(int i=0;i<n;i++)
        {
            grid[i]=(char*)malloc((n+1)*sizeof(char));
            scanf("%s",grid[i]);
        }
        cavityMap(n,grid);
        for(int i=0;i<n;i++)
            free(grid[i]);
        free(grid);
        return 0;
    }
    
  • + 0 comments

    Python 3

    Isolated the inner grid and then checked with neighbors with inplace replacement in string

    def cavityMap(grid):
        n_row = len(grid)
        n_col = len(grid[0])
        result = grid[:]
    
        # Define the range for the inner grid
        mr = range(1, n_row - 1)
        mc = range(1, n_col - 1)
    
        for row in mr:
            for col in mc:
                # Check all neighbors
                if (grid[row][col] > grid[row][col+1] and
                    grid[row][col] > grid[row][col-1] and
                    grid[row][col] > grid[row-1][col] and
                    grid[row][col] > grid[row+1][col]):
                    # Modify the result grid
                    result[row] = result[row][:col] + "X" + result[row][col+1:]
    
        return result