Grid Challenge Discussions | Algorithms | HackerRank

Grid Challenge

Sort by

recency

|

600 Discussions

|

  • + 0 comments

    My solution, it might complex

    def gridChallenge(grid):
        # Sort each row
        for idx in range(len(grid)):
            strList = list(grid[idx])
            strList.sort()
            grid[idx] = "".join(strList)
        # Join all grid list to a single string
        strJoin = "".join(grid)
        gLength = len(grid)
    		# Create the column list based on grid length
        reverseGrid = [0] * len(grid)
        for i in range(len(strJoin)):
    		    # get the ascii code
            curr = ord(strJoin[i])
    				# Get the saved grid ascii code based on index % length
            prev = reverseGrid[i % gLength]
            diff = curr - prev
    				# if the diff < 0, means current ascii value less than previous, which means it's not in order
            if diff < 0:
                return "NO"
    
            reverseGrid[i % gLength] = curr
        return "YES" 
    
  • + 0 comments

    python easy solution:

    def gridChallenge(grid):
        # Sort each row
        sorted_grid = [''.join(sorted(row)) for row in grid]
        
        # Zip to get columns
        for col in zip(*sorted_grid):
            if list(col) != sorted(col):
                return "NO"
        return "YES"
    
  • + 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";
    }