FastPrepFastPrep
Problem Brief

Diagonal Traverse (for E4 ;)

FULLTIMEPHONE SCREEN

LC 498~ The other question was LC 1539~~

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

1Example 1

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

Traverse the matrix diagonally as follows:

  1. Start from element 1
  2. Move up-right to reach 2, then down-left to 4
  3. Move up-right to reach 7, then down-right to 3
  4. Continue the pattern to traverse all elements diagonally
The resulting diagonal order is [1,2,4,7,3,5,8,6,9].

2Example 2

Input
mat = [[1,2],[3,4]]
Output
[1,2,3,4]
Explanation

Traverse the matrix diagonally as follows:

  1. Start from element 1
  2. Move up-right to reach 2, then down-left to 3
  3. Finally, move up-right to reach 4
The resulting diagonal order is [1,2,3,4].

Constraints

Limits and guarantees your solution can rely on.

  • m == mat.length
  • n == mat[i].length
  • 1 <= m, n <= 10⁴
  • 1 <= m * n <= 10⁴
  • -10⁵ <= mat[i][j] <= 10⁵
public int[] findDiagonalOrder(int[][] mat) {
  // write your code here
}
Input

mat

[[1,2,3],[4,5,6],[7,8,9]]

Output

[1,2,4,7,3,5,8,6,9]

Sign in to submit your solution.