#208MediumMatrix Traversal· Zigzag Anti-Diagonal Traversal
Diagonal Snake Order
Input format
`grid`: a non-empty rectangular matrix of integers.
Output format
An array with all `rows * cols` labels in snake order.
Examples
Input: grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Output: [1, 2, 4, 7, 5, 3, 6, 8, 9]
Diagonal sums 0 to 4 give: [1], then [2, 4] (down-left), then [7, 5, 3] (up-right), then [6, 8] (down-left), then [9].
Input: grid = [[1, 2], [3, 4]]
Output: [1, 2, 3, 4]
The diagonals are [1], [2, 3] and [4], so the order is 1, 2, 3, 4.
Input: grid = [[5], [6], [7]]
Output: [5, 6, 7]
A single column has one cell per diagonal, so the order is simply top to bottom.
Constraints
- 1 <= rows, cols <= 300
- -1000 <= grid[r][c] <= 1000
- #matrix
- #traversal
- #simulation
- #diagonals