DEV Community

Ronaldo Peres
Ronaldo Peres

Posted on

Days of Code [4] - Diagonal Traverse

Hi everyone,

Today I got this problem:

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

Constraints:

  • m == mat.length
  • n == mat[i].length
  • 1 <= m, n <= 104
  • 1 <= m * n <= 104
  • -105 <= mat[i][j] <= 105

The object is to get the numbers in the array going up and down in diagonal.

Example:

  • Input: mat = [[1,2,3],[4,5,6],[7,8,9]]
  • Output: [1,2,4,7,5,3,6,8,9]

Another:

  • Input: mat = [[5,6]]
  • Output: [5,6]

One more:

  • Input: mat = [[1,2,3,4],[5,6,7,8]]
  • Output: [1,2,5,6,3,4,7,8]

Here is one solution:

Diagonal Traverse

Top comments (0)