Grid Challenge Discussions | Algorithms | HackerRank

Grid Challenge

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