Solution: Rotate Image

Let's solve the Rotate Image problem using the Matrices pattern.

Statement#

Given an n×nn \times n matrix, rotate the matrix 90 degrees clockwise. The performed rotation should be in place, i.e., the given matrix is modified directly without allocating another matrix.

Note: The function should only return the modified input matrix.

Constraints:

  • matrix.length == matrix[i].length
  • 11 \leq matrix.length 20\leq 20
  • 103-10^3 \leq matrix[i][j] 103\leq 10^3

Solution#

The idea is to make groups of four cells and rotate them by 90 degrees clockwise.

Here’s how the algorithm works:

  1. We run a loop where row ranges from 0 to n / 2.

    1. Within this loop, we run a nested loop where col ranges from row to n - row - 1. These loops traverse the groups of four cells in the matrix. In this nested loop, we perform three swaps:

      1. The value of the top-left cell is swapped with the value of the top-right cell.

      2. The value of the top-left cell is swapped with the value of the bottom-right cell.

      3. The value of the top-left cell is swapped with the value of the bottom-left cell.

    2. The current group of four cells has been rotated by 9090 degrees. We now move to the next iteration of the outer loop to rotate the next group.

  2. We repeat the process above until the whole matrix has been rotated.

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

Created with Fabric.js 3.6.6 We run a loop, where row ranges from 0 to (n / 2), and a nested loop, where col ranges from row to (n - row - 1).

1 of 26

Created with Fabric.js 3.6.6 row = 0, col = 0We will be rotating the highlighted groups of cells in the current iteration of the nested loop.

2 of 26

Created with Fabric.js 3.6.6 row = 0, col = 0We will be rotating the highlighted groups of cells in the current iteration of the nested loop.

3 of 26

Created with Fabric.js 3.6.6 row = 0, col = 0We swap the values of the top-left and bottom-rightcells in the current group.

4 of 26

Created with Fabric.js 3.6.6 row = 0, col = 0We swap the values of the top-left and bottom-rightcells in the current group.

5 of 26

Created with Fabric.js 3.6.6 row = 0, col = 1We move to the next group of 4 cells.

6 of 26

Created with Fabric.js 3.6.6 row = 0, col = 1We swap the values of the top-left and top-rightcells in the current group.

7 of 26

Created with Fabric.js 3.6.6 row = 0, col = 1We swap the values of the top-left and bottom-rightcells in the current group.

8 of 26

Created with Fabric.js 3.6.6 row = 0, col = 1 We swap the values of the top-left and bottom-leftcells in the current group.

9 of 26

Created with Fabric.js 3.6.6 row = 0, col = 2We move to the next group of 4 cells.

10 of 26

Created with Fabric.js 3.6.6 row = 0, col = 2 We swap the values of the top-left and top-rightcells in the current group.

11 of 26

Created with Fabric.js 3.6.6 row = 0, col = 2 We swap the values of the top-left and top-rightcells in the current group.

12 of 26

Created with Fabric.js 3.6.6 row = 0, col = 2 We swap the values of the top-left and top-rightcells in the current group.

13 of 26

Created with Fabric.js 3.6.6 row = 0, col = 3We move to the next group of 4 cells.

14 of 26

Created with Fabric.js 3.6.6 row = 0, col = 3 We swap the values of the top-left and top-rightcells in the current group.

15 of 26

Created with Fabric.js 3.6.6 row = 0, col = 3We swap the values of the top-left and bottom-rightcells in the current group.

16 of 26

Created with Fabric.js 3.6.6 row = 0, col = 3We swap the values of the top-left and bottom-leftcells in the current group.

17 of 26

Created with Fabric.js 3.6.6 row = 1, col = 1We move to the next group of 4 cells.

18 of 26

Created with Fabric.js 3.6.6 row = 1, col = 1We swap the values of the top-left and top-rightcells in the current group.

19 of 26

Created with Fabric.js 3.6.6 row = 1, col = 1 We swap the values of the top-left and bottom-rightcells in the current group.

20 of 26

Created with Fabric.js 3.6.6 row = 1, col = 1We swap the values of the top-left and bottom-leftcells in the current group.

21 of 26

Created with Fabric.js 3.6.6 row = 1, col = 2We move to the next group of 4 cells.

22 of 26

Created with Fabric.js 3.6.6 row = 1, col = 2 We swap the values of the top-left and top-rightcells in the current group.

23 of 26

Created with Fabric.js 3.6.6 row = 1, col = 2 We swap the values of the top-left and bottom-rightcells in the current group.

24 of 26

Created with Fabric.js 3.6.6 row = 1, col = 2We swap the values of the top-left and bottom-leftcells in the current group.

25 of 26

Created with Fabric.js 3.6.6 The middle cell will remain in the same position, so itis excluded from the operations performed in the loops. The matrix has been rotated by 90 degrees clockwise, so we return the matrix.

26 of 26

Let’s have a look at the code for the algorithm we just discussed:

Rotate Image

Time complexity#

The time complexity of this solution is O(n2)O(n^2) since each cell is read and written once.

Space complexity#

The space complexity of this solution is O(1)O(1) since no extra space is used.

Rotate Image

Spiral Matrix