Grid Challenge Discussions | | HackerRank

Grid Challenge

Sort by

recency

|

442 Discussions

|

  • + 0 comments

    Javascript

    function gridChallenge(grid: string[]): string { // Write your code here const sorted_grid = grid.map(row => Array.from(row).sort())

    for(let j=0; j<sorted_grid.length; j++){
        for(let i=0; i <sorted_grid.length-1; i++){
          if (sorted_grid[i+1][j] < sorted_grid[i][j]){
              return 'NO'
          }   
        }
    }
    
    return 'YES'
    

    }

  • + 0 comments

    The problem description incorrectly uses the term ascending. Ascending is not the same as non-decreasing.

  • + 0 comments

    Test case 12 is invalid. The documentation says that its a square grid, but test case 12 has a 3x4 and 4x3 grid as the 2nd and 3rd test case:

    3 3 abc lmp qrt 3 mpxz abcd wlmf 4 abc hjk mpq rtv

  • + 0 comments

    Java 15 using streams, some utils and early return for NO result:

    class Cache<K, V> {
        protected final HashMap<K, V> storage = new HashMap<>();
        
        public Function<K, V> memoize(Function<K, V> func) {
            return k -> storage.computeIfAbsent(k, func);
        }
    }
    
    class StreamUtils {
        public static <T> Stream<ArrayList<T>> sliding(Stream<T> stream, int n) {
            // Use LinkedList to store the sliding window
            var window = new LinkedList<T>();
    
            // Use flatMap to emit the window after each element
            return stream.flatMap(curr -> {
                window.add(curr); // Add current element to window
                if (window.size() > n) {
                    window.removeFirst(); // Remove oldest element
                }
                if (window.size() == n) {
                    return Stream.of(new ArrayList<>(window)); // Emit window
                }
                return Stream.empty(); // Skip until window is full
            });
        }
    
        public static <T> boolean isSorted(Stream<T> stream, Comparator<? super T> comparator) {
            return StreamUtils.sliding(stream, 2).allMatch(window -> {
                return comparator.compare(window.get(0), window.get(1)) <= 0;
            });
        }
    }
    
    class Result {
    
        public static String gridChallenge(List<String> grid) {
            var nRows = grid.size();
            assert 1 <= nRows && nRows <= 100;
            // the question lies about input being square - nRows != nCols in third test case;
            // we need to find nCols separately
            var nCols = grid.get(0).length();
            
            var getSortedRow = new Cache<Integer, String>().memoize(i -> {
                var row = grid.get(i);
                var sorted = row.chars().sorted().collect(
                    StringBuilder::new,
                    StringBuilder::appendCodePoint,
                    StringBuilder::append
                ).toString();
                return sorted;
            });
            
            var cols = IntStream.range(0, nCols).mapToObj(j ->
                IntStream.range(0, nRows).map(i -> getSortedRow.apply(i).charAt(j))
            );
            var allColsSorted = cols.allMatch(col ->
                StreamUtils.isSorted(col.boxed(), Comparator.naturalOrder())
            );
                
            return allColsSorted ? "YES" : "NO";
        }
    }
    
  • + 0 comments

    my solution in python3 .. might not be the fastest but passes all tests..

    def gridChallenge(grid): rows = len(grid) cols = len(grid[0])

    for i in range(rows):
        grid[i] = sorted(grid[i])
    
    for j in range(cols):
        for i in range(rows-1):
            if grid[i][j] > grid[i+1][j]:
                return 'NO'
    return 'YES'