Diagonal Traverse

Given an m x n matrix mat, return an array of all the elements of the array in a diagonal order.

Example 1
1 2 3
4 5 6
7 8 9
Inputmat = [[1,2,3],[4,5,6],[7,8,9]]
Output[1,2,4,7,5,3,6,8,9]
The matrix elements are visited along diagonals in alternating directions, producing the shown order.
Example 2
1 2
3 4
Inputmat = [[1,2],[3,4]]
Output[1,2,3,4]
The 2 x 2 matrix is traversed diagonally to produce the shown order.

Constraints

  • m == mat.length
  • n == mat[i].length
  • 1 <= m, n <= 10^4
  • 1 <= m * n <= 10^4
  • -10^5 <= mat[i][j] <= 10^5

Asked at 12 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate