Solution: Spiral Matrix II

Let’s solve the Spiral Matrix II problem using the Matrices pattern.

Statement

Given a positive integer n, create an n×nn \times n, matrix where the elements are sequential integers from 11 to n2n^2, arranged in a spiral pattern.

Constraints:

  • 1 \leqn \leq2020

Solution

The essence of this solution lies in systematically filling an n×nn×n matrix in a spiral order by iterating layer by layer. The core idea is to divide the matrix into layers and traverse each layer in four directional steps (right, down, left, and up), filling the matrix cells with sequential numbers. This approach ensures the matrix is filled in a spiral pattern.

Now, let’s look at the steps of the solution:

  1. We initialize a matrix of size n×nn×n with zeros and a counter, count with 11. This matrix serves as the container for the spiral pattern and the counter to keep track of the current number to be placed in the matrix.

  2. We iterate through all the layers. The number of layers is calculated as (n+1)//2(n+1)//2, which represents the number of complete iterations required to fill the matrix in a spiral order. Each layer is processed one at a time.

  3. For each layer, the matrix cells are filled in four specific directions:

    1. Direction 1 (left to right): Traverse the top row of the current layer, filling the numbers from layer to n−layer. We start from the leftmost cell of the current layer’s top row, traverse toward the right, and fill in the numbers. For example, in the first layer of a 3×33\times3 matrix, fill the first row with 11, 22, and 33.

    2. Direction 2 (top to bottom): Traverse the right column of the current layer, starting from the row below the top row(layer+1) to the bottom row of the current layer(n-layer). Here, we traverse downwards, filling in the numbers. Continuing from the previous example, place 44 and 55 in the right column.

    3. Direction 3 (right to left): Traverse the bottom row of the current layer, moving from the rightmost cell to the leftmost cell. Here, we start from the rightmost cell of the bottom row and traverse toward the left, filling in the numbers. This ensures the bottom row of the current layer is filled. For instance, place 66 and 77.

    4. Direction 4 (bottom to top): Traverse the left column of the current layer, moving upward from the cell below the bottom row to the cell above the top row. We start filling in the numbers upwards in the leftmost column. For example, place 88.

  At the end of the layer, all cells for that layer are filled in spiral order. For example, 99.

  1. The process is repeated for the next inner layer, if any, until the entire matrix is filled. For example, in the case of a 3×33\times3 matrix, the inner layer (center cell) is filled as the final step.

  2. Once all layers are processed, the filled n×nn \times n matrix is returned.

Let’s look at the following illustration to get a better understanding of the solution:

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.