498. Diagonal Traverse
Difficulty: Medium
Topics: Array
, Matrix
, Simulation
Given an m x n
matrix mat
, return an array of all the elements of the array in a diagonal order.
Example 1:
- Input: mat = [[1,2,3],[4,5,6],[7,8,9]]
- Output: [1,2,4,7,5,3,6,8,9]
Example 2:
- Input: mat = [[1,2],[3,4]]
- Output: [1,2,3,4]
Constraints:
m == mat.length
n == mat[i].length
1 <= m, n <= 104
1 <= m * n <= 104
-105 <= mat[i][j] <= 105
Solution:
We need to traverse a given matrix in a diagonal order, alternating the direction of traversal for each diagonal. The key observation is that the sum of the indices (i + j) for each element in a diagonal is constant. For even-indexed diagonals (by sum), we traverse from the bottom to the top, and for odd-indexed diagonals, we traverse from the top to the bottom.
Approach
- Problem Analysis: The problem requires traversing a matrix diagonally, changing direction after each diagonal. The challenge is to handle the boundaries of the matrix correctly while alternating the traversal direction.
-
Key Insight: For each diagonal sum
s
(from 0 tom + n - 2
), ifs
is even, we start from the bottom-most valid row and move upwards. Ifs
is odd, we start from the right-most valid column and move leftwards and downwards. -
Algorithm:
- Iterate over each possible diagonal sum
s
. - For even
s
, start with the highest possible row index within bounds and move up, collecting elements until we go out of bounds. - For odd
s
, start with the highest possible column index within bounds and move down and left, collecting elements until we go out of bounds.
- Iterate over each possible diagonal sum
- Complexity Analysis: The algorithm visits each element exactly once, resulting in a time complexity of O(m * n), which is optimal. The space complexity is O(1) excluding the output array.
Let's implement this solution in PHP: 498. Diagonal Traverse
<?php
/**
* @param Integer[][] $mat
* @return Integer[]
*/
function findDiagonalOrder($mat) {
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
// Example 1:
$mat1 = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
print_r(findDiagonalOrder($mat1));
// Expected: [1,2,4,7,5,3,6,8,9]
// Example 2:
$mat2 = [
[1, 2],
[3, 4]
];
print_r(findDiagonalOrder($mat2));
// Expected: [1,2,3,4]
?>
Explanation:
-
Initialization: We determine the dimensions of the matrix
m
(number of rows) andn
(number of columns). -
Diagonal Sum Iteration: For each diagonal sum
s
from 0 tom + n - 2
:-
Even
s
: We start from the bottom-most valid row (minimizings
andm-1
) and move upwards, collecting elements until we exceed the matrix boundaries. -
Odd
s
: We start from the right-most valid column (minimizings
andn-1
) and move downwards and leftwards, collecting elements until we exceed the matrix boundaries.
-
Even
- Result Construction: The collected elements are stored in the result array, which is returned after processing all diagonals.
This approach efficiently collects all elements in the required diagonal order by leveraging the constant sum of indices for each diagonal and alternating traversal directions. The solution handles all edge cases, including non-square matrices, seamlessly.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
Top comments (0)