Grid Challenge Discussions | Algorithms | HackerRank

Grid Challenge

Sort by

recency

|

594 Discussions

|

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

    Here is my Python solution!

    def gridChallenge(grid):
        grid = [sorted(list(row)) for row in grid]
        for col in range(len(grid[0])):
            for row in range(1, len(grid)):
                if grid[row][col] < grid[row - 1][col]:
                    return "NO"
        return "YES"
    
  • + 0 comments

    PYTHON

    def gridChallenge(grid):
        # Write your code here
        for i in range(0,len(grid)):
            grid[i]=''.join(sorted(grid[i]))
        
        for col in range(0,len(grid[0])):
            anch=-999
            for row in range(0,len(grid)):
                if anch> ord(grid[row][col]) :
                    return "NO"
                anch=ord(grid[row][col])
        
        return "YES"
    
  • + 0 comments

    public static String gridChallenge(List grid) { for (int i =0;i0){ return "NO"; } } return "YES"; }

    }

  • + 0 comments

    Haskell

    module Main where
    
    import Control.Monad (replicateM, replicateM_)
    import Data.List (sort, transpose)
    
    solve :: (Ord a) => [[a]] -> Bool
    solve grid = all (\xs -> xs == sort xs) $ transpose $ map sort grid
    
    main :: IO ()
    main = do
        cases <- readLn :: IO Int
        replicateM_
            cases
            ( do
                n <- readLn :: IO Int
                grid <- replicateM n getLine
                putStrLn $ if solve grid then "YES" else "NO"
            )