Sort by

recency

|

853 Discussions

|

  • + 0 comments

    Solution from my side, i know that it is not well oprimized. but the logic of this solution is in the understandable format.

    def cavityMap(grid):
        data = []
        result = []
        indexData = []
    
        for item in grid:
            data.append(list(item))
    
        for i in range(1,len(data)-1):
            for j in range(1,len(data)-1):
                if(data[i][j+1]<data[i][j]> data[i][j-1] and data[i+1][j]<data[i][j]> data[i-1][j]):
                    indexData.append([i,j])
    
        for item01 in indexData:
            data[item01[0]][item01[1]] = 'X'
    
        for item03 in data:
            result.append(''.join(item03))
            
        return result
    
  • + 0 comments
    for(int i = 1; i< grid.size()-1; i++){
            for(int j = 1; j < grid.size()-1; j++){
                if(grid[i-1][j] < 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][j] = 'X';
                   }
            }
        }
        return grid;
    
  • + 1 comment

    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

    A solution in C# that is more slightly more optimized than the rest I've seen here. Because of how a cavity is defined in this, there are two conditions where you can skip evaluating the four adjacent locations:

    1. If the element preceding the current one in this row was a cavity, this one cannot be. We can use this to skip the next position by incrementing our loop an additional time while replacing the character in the string.
    2. If the element at this position in the previous row was a cavity, this one cannot be. We can take advantage of the short-circuit evaluation of C# to reduce the number of comparison operations by placing the [i - 1] [j] check first, as that will always be false if the cell at that position was changed to 'X'. (Note: not all languages use short-circuit evaluation, so for some languages this will change nothing)

    Combined, these two changes can remove up to approximately 3.5 * ((N - 2) ^ 2) / 2 comparison operations from our execution time!

    public static List<string> cavityMap(List<string> grid)
        {
            for (int i = 1; i < grid.Count - 1; i++)
            {
                for (int j = 1; j < grid[i].Length - 1; j++)
                {
                    if (grid[i].ElementAt(j) > grid[i - 1].ElementAt(j) &&
                        grid[i].ElementAt(j) > grid[i + 1].ElementAt(j) &&
                        grid[i].ElementAt(j) > grid[i].ElementAt(j - 1) &&
                        grid[i].ElementAt(j) > grid[i].ElementAt(j + 1))
                    {
                        
                        grid[i] = grid[i].Substring(0, j) + "X" + grid[i].Substring(++j);
                    }
                }
            }
            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;
    
        }