← Whiteboard questions

Rotate Image

Turn a square picture a quarter turn clockwise

DSA, Medium, Math & Geometry

Which structure or algorithm, and what does it cost.

A square picture is stored as a grid grid with n rows and n columns. Each cell holds the brightness of one pixel. Write rotate(grid) to turn the picture 90 degrees clockwise. Change grid in place, with no second grid, and return it. After the turn, the old left column becomes the top row, read from bottom to top.

Examples

Input grid = [[5, 0, 2], [7, 1, 9], [3, 8, 4]]
Output [[3, 7, 5], [8, 1, 0], [4, 9, 2]]
Why the left column 5, 7, 3 read from the bottom is 3, 7, 5, and it becomes the top row.
Input grid = [[6]]
Output [[6]]
Why a picture with one pixel looks the same after any turn.
Input grid = [[0, 1, 0, 1], [2, 2, 3, 3], [4, 5, 6, 7], [8, 8, 9, 9]]
Output [[8, 4, 2, 0], [8, 5, 2, 1], [9, 6, 3, 0], [9, 7, 3, 1]]
Why an even size has no centre cell, so every cell moves to a new place.

Constraints

Your answer must contain

Target Aim for O(n^2) time and O(1) extra memory.

Hints

Try the problem first. Open one hint at a time.

Hint 1 a nudge

Rotate a small grid on paper and watch where one corner cell goes. Where does the cell in row r and column c land?

Hint 2 the approach

A quarter turn can be made from two simple mirror-type moves in a row. Think about what each kind of mirror does to a cell's row and column.

Hint 3 the key step

Swap each cell with its mirror across the diagonal from top-left to bottom-right, once per pair. Then reverse every row. Together they give a clockwise turn, with no second grid.

Walkthrough

Full walkthrough brute force to best

Brute force

Make a second grid. The cell at row r and column c moves to row c and column n - 1 - r. Fill the new grid, then copy it back. That is O(n^2) time and O(n^2) extra memory, which breaks the in-place rule.

Best method

Flip the grid across its main diagonal, the line from the top-left corner to the bottom-right corner: swap each cell with the cell at the swapped row and column. This sends row r, column c to row c, column r. Then reverse every row, which sends column r to column n - 1 - r. Together a cell goes from (r, c) to (c, n - 1 - r), and that is exactly a clockwise quarter turn. Each pair is swapped once, so no second grid is needed.

Hand trace

Run on [[5, 0, 2], [7, 1, 9], [3, 8, 4]]. Step 1, swap across the diagonal: row 0, column 1 with row 1, column 0 (0 and 7); row 0, column 2 with row 2, column 0 (2 and 3); row 1, column 2 with row 2, column 1 (9 and 8). The grid is [[5, 7, 3], [0, 1, 8], [2, 9, 4]]. Step 2, reverse each row: [3, 7, 5], [8, 1, 0], [4, 9, 2]. The result is [[3, 7, 5], [8, 1, 0], [4, 9, 2]].

Code

def rotate(grid):
    n = len(grid)
    for r in range(n):
        for c in range(r + 1, n):
            grid[r][c], grid[c][r] = grid[c][r], grid[r][c]
    for row in grid:
        row.reverse()
    return grid

Time and memory

O(n^2) time, because each cell is touched a fixed number of times. O(1) extra memory, because every swap happens inside the grid.

Common mistakes

  • Swapping every pair in the first step, not only the cells above the diagonal. Each pair then swaps twice and the grid comes back unchanged.
  • Reversing the order of the rows instead of the cells inside each row. That gives a flip, not a turn.
  • Building a new grid and assigning it to the name grid inside the function. The caller's grid is not changed.
  • Reversing each row first and then flipping across the diagonal. That turns the picture anticlockwise.

Related problems

More Math & Geometry problems
Rotate Image: hints, walkthrough and Python solution