• + 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