Grid Challenge Discussions | | HackerRank

Grid Challenge

  • + 0 comments

    [javascript]

    The one gotcha here is that the prompt is incorrect: a SQUARE grid is not guaranteed.

    Order the rows as requested, then check "vertically" for top-down ascending order of columns as well.

    While this approach is fairly brute-force, it returns as soon as it finds the current row to be "greater than" the row below it.

    function gridChallenge(grid) {
        // Should order each row in a one-liner
        const ordered = grid.map((row) => row.split('').sort().join(''));
        console.log(ordered);
        // operate on row-level
        for (let i = 0; i < grid.length - 1; i++) {
            // column level
            for (let j = 0; j < grid[0].length; j++) {
                let cur = ordered[i][j];
                let next = ordered[i + 1][j];
                if (cur > next) {
                    return "NO";
                }
            }
        }
        return "YES";
    }