0% completed
Problem Statement
Given a 2D matrix of size m x n
, return a 1D array containing elements of the matrix in the diagonal order.
Example 1:
- Input: `matrix =
[[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]]
- Expected Output:
[1, 2, 5, 9, 6, 3, 4, 7, 10, 13, 14, 11, 8, 12, 15, 16]
- Justification: Traversal begins at 1, moves to 2, diagonally down to 5, and so on.
Example 2:
- Input: matrix =
[[1,5],
[3,6]]
- Expected Output:
[1,5,3,6]
- Justification: The traversal starts from 1, moves to 2, then down to 3, and finally to 4.
Example 3:
- Input: `matrix =
[[1],
[2],
[3]]
- Expected Output:
[1,2,3]
- Justification: The traversal is straightforward down the single-column matrix.
Solution
To solve this problem, we need to cleverly navigate the 2D array. The key is to recognize the pattern of movement: up-right and down-left diagonals, alternating after hitting the boundaries. We utilize two pointers to track our current position in the matrix and a boolean flag to indicate the current direction of movement. This approach ensures we cover all elements in the required order.
By dynamically altering our direction at the array's boundaries, we effectively traverse all elements in the zigzag pattern. This method is efficient as it requires just a single pass through the matrix, making it a time-efficient solution. Moreover, it's intuitive as it closely follows the pattern described in the problem statement.
Step-by-step Algorithm
- Initialize pointers for
row
andcolumn
, and a booleanflag
for the direction of movement. - Iterate through the matrix elements until all elements are traversed.
- If moving upwards:
- Move diagonally
up-right
by decrementing the row and incrementing the column. - Check if the
boundary
is reached. If so, change direction and adjust row/column accordingly.
- Move diagonally
- If moving downwards:
- Move diagonally
down-left
by incrementing the row and decrementing the column. - Check if the boundary is reached. If so, change direction and adjust row/column accordingly.
- Move diagonally
- If moving upwards:
- Throughout the traversal, add the current element to the result list.
- Return the result list after complete traversal.
Algorithm Walkthrough
- Given Input:
matrix
=
[[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]]
-
Start:
row = 0
,column = 0
,upwards = true
,result = []
.- Add
1
.result = [1]
.
- Add
-
Change diagonal: Hit boundary. Change direction.
row = 0
,column = 1
,upwards = false
.- Add
2
.result = [1, 2]
.
- Add
-
Move Down-Left: Within bounds.
row = 1
,column = 0
.- Add
5
.result = [1, 2, 5]
.
- Add
-
Change diagonal: Hit boundary. Change direction.
row = 2
,column = 0
,upwards = true
.- Add
9
.result = [1, 2, 5, 9]
.
- Add
-
Move Up-Right: Within bounds.
row = 1
,column = 1
.- Add
6
.result = [1, 2, 5, 9, 6]
.
- Add
-
Change diagonal: Hit boundary. Change direction.
row = 0
,column = 2
,upwards = false
.- Add
3
.result = [1, 2, 5, 9, 6, 3]
.
- Add
-
Move Down-Left: Within bounds.
row = 0
,column = 3
.- Add
4
.result = [1, 2, 5, 9, 6, 3, 4]
.
- Add
-
Move Down-Left: Within bounds.
row = 1
,column = 2
.- Add
7
.result = [1, 2, 5, 9, 6, 3, 4, 7]
.
- Add
-
Move Up-Right: Within bounds.
row = 2
,column = 1
.- Add
10
.result = [1, 2, 5, 9, 6, 3, 4, 7, 10]
.
- Add
-
Change diagonal: Hit boundary. Change direction.
row = 3
,column = 0
,upwards = false
.- Add
13
.result = [1, 2, 5, 9, 6, 3, 4, 7, 10, 13]
.
- Add
-
Move Down-Left: Within bounds.
row = 3
,column = 1
.- Add
14
.result = [1, 2, 5, 9, 6, 3, 4, 7, 10, 13, 14]
.
- Add
-
Move Down-Left: Within bounds.
row = 2
,column = 2
.- Add
11
.result = [1, 2, 5, 9, 6, 3, 4, 7, 10, 13, 14, 11]
.
- Add
-
Change diagonal: Hit boundary. Change direction.
row = 1
,column = 3
.upwards = false
.- Add
8
.result = [1, 2, 5, 9, 6, 3, 4, 7, 10, 13, 14, 11, 8]
.
- Add
-
Move Up-Right: Within bounds.
row = 3
,column = 2
.- Add
12
.result = [1, 2, 5, 9, 6, 3, 4, 7, 10, 13, 14, 11, 8, 12]
.
- Add
-
Change diagonal: Hit boundary. Change direction.
row = 3
,column = 2
,upwards = true
.- Add
15
.result = [1, 2, 5, 9, 6, 3, 4, 7, 10, 13, 14, 11, 8, 12, 15]
.
- Add
-
Change diagonal: Hit boundary. End of matrix.
row = 3
,column = 3
,upwards = false
.- Add
16
. Finalresult = [1, 2, 5, 9, 6, 3, 4, 7, 10, 13, 14, 11, 8, 12, 15, 16]
.
- Add
The final output is [1, 2, 5, 9, 6, 3, 4, 7, 10, 13, 14, 11, 8, 12, 15, 16]
.
Code
Complexity Analysis
Time Complexity
The time complexity of the algorithm is O(M x N)
, where M is the number of rows, and N is the number of columns in the matrix. The algorithm traverses each element once, so the time complexity is proportional to the total number of elements, which is M x N
.
Space Complexity
The space used is for the output list, which holds all elements of the matrix. Since there are M x N
elements, the space complexity is O(M x N)
.
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible