FunctionRacerProblemsRace

Zigzag Diagonals

MediumGridsPython, JavaScript, TypeScript, Java, C++

Walk through grid along its anti-diagonals, the sets of cells whose row index plus column index is the same, starting with the top-left cell. On the first anti-diagonal you visit that single cell. On the next one you move from its top-right end to its bottom-left end (row increasing), on the one after that from its bottom-left end to its top-right end (row decreasing), and you keep alternating like this until the bottom-right cell. Return the values in the order you visit them.

Example 1

grid [[1,2,3],[4,5,6],[7,8,9]]
returns [1,2,4,7,5,3,6,8,9]

The anti-diagonals are [1], [2, 4], [3, 5, 7], [6, 8], [9]; the second and fourth are walked with the row increasing, the third with the row decreasing, giving 1, 2, 4, 7, 5, 3, 6, 8, 9.

Example 2

grid [[1,2,3,4],[5,6,7,8]]
returns [1,2,5,6,3,4,7,8]

Anti-diagonals [1], [2, 5], [3, 6], [4, 7], [8] walked in alternating directions give 1, 2, 5, 6, 3, 4, 7, 8.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Grids problems