Grid Challenge Discussions | | HackerRank

Grid Challenge

Sort by

recency

|

474 Discussions

|

  • + 0 comments

    Python

    Even better option: 1.3x faster

    def gridChallenge(grid):
        # rearrange elements for rows
        rows = [sorted(list(row_str)) for row_str in grid]
    
        # check for columns order
        for col in zip(*rows):
            for j in range(len(col)-1):
                if col[j] > col[j+1]:
                    return 'NO'
        return 'YES'
    
  • + 2 comments

    Python

    def gridChallenge(grid):
        rows = len(grid)
        long = len(grid[0])
        mx = [list(row) for row in grid]
        
        # rearrange elements for rows
        for idx, row in enumerate(mx):
            mx[idx] = sorted(row)
            
        # check for columns order
        for i in range(long):
            checking = []
            for row in mx:
                checking.append(row[i])
            print(i,checking,sep=":")
            for j in range(rows-1):
                if checking[j] > checking[j+1]:
                    return 'NO'
        return 'YES'
    
  • + 0 comments

    "Sample Test Case 1" has an input that does NOT match the constraints in the problem description.

    Specifically: There will be N strings each of which will be of length N. This implies the grid is always square.

    The aformentioned test case contains the following input: 4 abc hjk mpq rtv

    4 strings, each with 3 characters.

    Please either correct your test case or the problem description.

    (also please fix these text boxes which are not expanable nor scrollable past 10 lines on the latest firefox version as of today (been like this for as long as I've been using HR the last few months AFAIK). I have no choice but to type in something else, then paste.)

  • + 0 comments

    My Java 8 Solution

    public static String gridChallenge(List<String> grid) {
            for (int i = 0; i < grid.size(); i++) {
                char[] ca = grid.get(i).toCharArray();
                Arrays.sort(ca);
                grid.set(i, String.valueOf(ca));
            }
            
            for (int j = 0; j < grid.get(0).length(); j++) {
                for (int i = 1; i < grid.size(); i++) {
                    if (grid.get(i).charAt(j) < grid.get(i - 1).charAt(j)) return "NO";
                }
            }
            
            return "YES";
        }
    
  • + 1 comment

    No clever way of solving this; You have to iterate through the list, convert each string into a char array, sort them, create a new sorted gridand than iterate through the new grid with a nested for loop, and and check if the vertical lines are in order.