Solution: Spiral Matrix
Let's solve the Spiral Matrix problem using the Matrices pattern.
We'll cover the following
Statement#
Given an matrix, return an array containing the matrix elements in spiral order, starting from the top-left cell.
Constraints:
-
matrix.length -
matrix[i].length -
matrix[i][j]
Solution#
We’ll maintain a variable, direction, which indicates the direction we need to travel in. It will store the value of either or . Its value will change based on the following rules:
-
It will be if we’re traveling in the following directions:
- Left to right (horizontal)
- Top to bottom (vertical)
-
It will be if we’re traveling in the following directions:
- Right to left (horizontal)
- Bottom to top (vertical)
Here’s how the algorithm works:
-
We initialize the following variables:
rows, cols = len(matrix), len(matrix[0]): These are the total number of rows and columns in the matrix, respectively.row, col = 0, -1: These are the pointers used to traverse the matrix.direction = 1: This indicates the direction we need to travel in. It is initialized to since our first traversal will be in the left to right (horizontal) direction.result = []: This array stores the matrix elements in spiral order.
-
We start the traversal from the top left cell in the matrix:
- We first travel from left to right in a row by adding the
directionvariable tocolwhile keeping the value ofrowunchanged. At each iteration, we will addmatrix[row][col]toresult. At the end of this row traversal, we decrementrowsby because if we traverse a column after this, we’ll traverse one less element. - Next, we travel from top to bottom in a column by adding the
directionvariable torowwhile keeping the value ofcolunchanged. At each iteration, we will addmatrix[row][col]toresult. At the end of this column traversal, we decrementcolsby because if we traverse a row after this, we’ll traverse one less element.
- We first travel from left to right in a row by adding the
-
We reverse the direction by multiplying the
directionvariable by :-
Now, we travel from right to left in a row by adding the
directionvariable (which is now ) tocolwhile keeping the value ofrowunchanged. At each iteration, we will addmatrix[row][col]toresult. At the end of this row traversal, we decrementrowsby because if we traverse a column after this, we’ll traverse one less element. -
Next, we travel from bottom to top in a col by adding the
directionvariable (which is now ) torowwhile keeping the value ofcolunchanged. At each iteration, we will addmatrix[row][col]toresult. At the end of this column traversal, we decrementcolsby because if we traverse a row after this, we’ll traverse one less element.
-
-
We again reverse the direction by multiplying the
directionvariable by . -
The four steps above are repeated until all the cells of the matrix have been traversed, after which
resultstores the spiral order, so we returnresult.
Let’s look at the following illustration to get a better understanding of the solution:
1 of 20
2 of 20
3 of 20
4 of 20
5 of 20
6 of 20
7 of 20
8 of 20
9 of 20
10 of 20
11 of 20
12 of 20
13 of 20
14 of 20
15 of 20
16 of 20
17 of 20
18 of 20
19 of 20
20 of 20
Let’s have a look at the code for the algorithm we just discussed:
Spiral Matrix
Final Remarks