Grid Challenge Discussions | Algorithms | HackerRank

Grid Challenge

Sort by

recency

|

598 Discussions

|

  • + 0 comments

    It's NOT square matrix. Problem Description is Bad

  • + 0 comments
    def gridChallenge(grid):
        grid = [sorted(s) for s in grid]   
         
        for c in range(len(grid[0])):
            c_str = [row[c] for row in grid]
            
            for i in range(1, len(c_str)):
                if c_str[i-1] > c_str[i]:
                    return "NO"
                
        return "YES" 
    
  • + 0 comments

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

    string gridChallenge(vector<string> grid) {
        for(int i = 0; i < grid.size(); i++) sort(grid[i].begin(), grid[i].end());
        for(int col = 0; col < grid.size(); col++){
           for(int row = 1; row < grid.size(); row++)
                if(grid[row][col] < grid[row-1][col]) return "NO";
        }
        return "YES";
    }
    
  • + 0 comments

    My Java solution with o(r * c log c) time complexity and o(r) space:

    public static String gridChallenge(List<String> grid) {
            if(grid.size() == 1) return "YES"; //no cols to check
            
            // 1. sort rows alphabetically ascending
            List<String> sortedGrid = new ArrayList<>();
            for(String row : grid){
                char[] chars = row.toCharArray();
                Arrays.sort(chars);
                sortedGrid.add(new String(chars));
            }
            
            // 2. check cols for alphabetically ascending order from top to bottom
            for(int col = 0; col < grid.get(0).length(); col++){
                for(int row = 1; row < grid.size(); row++){
                    String currRow = sortedGrid.get(row);
                    String prevRow = sortedGrid.get(row - 1);
                    //check if col is in order
                    if(currRow.charAt(col) < prevRow.charAt(col)) return "NO";
                }
            }
            return "YES";
        }
    
  • + 0 comments

    Java //O(n*n)

    public static String gridChallenge(List<String> grid) {
            int n = grid.size();
            int m = grid.get(0).length();
            char[][] matrix= new char[n][m];
            for(int i=0; i < n; ++i) {
                    matrix[i] = grid.get(i).toCharArray();
                    Arrays.sort(matrix[i]);
            }
            for(int j=0; j < m; ++j) {
                    for(int i=1; i < n; ++i) {
                            if (matrix[i][j] < matrix[i-1][j]) {
                                    return "NO";
                            }
                    }        
            }
            return "YES";
    }