Given an m x n grid matrix, return all of its elements in zigzag anti-diagonal order: walk the anti-diagonals (cells with the same row + col) starting at the top-left cell, going up-right on the first diagonal, then down-left on the next, alternating.
Examples
Input: matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
Output: [1, 2, 5, 9, 6, 3, 4, 7, 10, 11, 8, 12]
Explanation: the diagonals, in the order they are read, are [1], [2, 5], [9, 6, 3],
[4, 7, 10], [11, 8], [12] - the second is read downwards, the third
upwards, and so on.
Input: matrix = [[1, 2], [3, 4], [5, 6]]
Output: [1, 2, 3, 5, 4, 6]
Constraints
1 <= m, n <= 500- Target: O(m * n) time.
Goals
- Group cells by the sum of their row and column indices
- Alternate the direction of traversal on consecutive diagonals
- Handle rectangular grids where diagonals are cut off by the borders