Given an `m x n` matrix `mat`, return all of its elements in diagonal order: start at the top-left, walk up and to the right along the first diagonal, then down and to the left along the next, and keep alternating direction until the bottom-right cell.
Given an m x n matrix mat, return all of its elements in diagonal order: start at the top-left, walk up and to the right along the first diagonal, then down and to the left along the next, and keep alternating direction until the bottom-right cell.
Example 1:
Input: {"mat":[[1,2,3],[4,5,6],[7,8,9]]}
Output: [1,2,4,7,5,3,6,8,9]
Constraints:
m==mat.length
n==mat[i].length
1≤m,n≤104
1≤m⋅n≤104
−105≤mat[i][j]≤105
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"mat":[[1,2,3],[4,5,6],[7,8,9]]}
Cells on one diagonal share r + c = d. There are 5 diagonals (d = 0 to 4); even ones go up-right, odd ones go down-left.