Rotate Image - Problem

๐Ÿ”„ Rotate Image - Transform Your Matrix Like a Pro!

Imagine you're working with digital image processing, and you need to rotate a square image by 90 degrees clockwise. You're given an n ร— n 2D matrix representing an image, where each cell contains a pixel value.

Your challenge? Rotate the entire image in-place - meaning you must modify the original matrix directly without using any additional 2D matrices. This constraint makes the problem particularly interesting as you need to be clever about how you move elements around!

Example: If you have a 3ร—3 matrix representing an image, after rotation:

[1,2,3]    [7,4,1]
[4,5,6] -> [8,5,2]
[7,8,9]    [9,6,3]

Notice how the first row becomes the last column, the second row becomes the middle column, and so on. Your task is to implement this transformation efficiently and elegantly!

Input & Output

example_1.py โ€” Basic 3ร—3 Matrix
$ Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
โ€บ Output: [[7,4,1],[8,5,2],[9,6,3]]
๐Ÿ’ก Note: After 90ยฐ clockwise rotation, the first row [1,2,3] becomes the last column [1,2,3], the second row [4,5,6] becomes the middle column [4,5,6], and the third row [7,8,9] becomes the first column [7,8,9]. The result is the matrix rotated 90ยฐ clockwise.
example_2.py โ€” 4ร—4 Matrix
$ Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
โ€บ Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
๐Ÿ’ก Note: For a 4ร—4 matrix, we have two layers to rotate. The outer layer elements move in cycles of 4, and the inner 2ร—2 layer also rotates. Each element follows the pattern: position (i,j) โ†’ (j, n-1-i).
example_3.py โ€” Single Element
$ Input: matrix = [[1]]
โ€บ Output: [[1]]
๐Ÿ’ก Note: A 1ร—1 matrix remains unchanged after rotation since there's only one element. This is a simple edge case that our algorithm should handle correctly.

Visualization

Tap to expand
Layer 1 (Outer)Layer 2 (Inner)ABCDAโ†’BBโ†’CCโ†’DDโ†’AStep-by-Step Processtemp = AA = DD = CC = BB = temp1. Save top-left element2. Move bottom-left to top-left3. Move bottom-right to bottom-left4. Move top-right to bottom-right5. Move saved to top-rightResult: All 4 elements rotated clockwise!Repeat for all 4-element groups in each layer๐Ÿ’ก Key Insight: Rotate 4 elements simultaneously to avoid overwriting!
Understanding the Visualization
1
Identify Layers
An nร—n matrix has n/2 concentric layers. The outermost layer forms the border, and we work inward.
2
Process Each Layer
For each layer, we identify the positions that need to rotate and group them into 4-element cycles.
3
4-Element Rotation
For each group of 4 positions, we save one element temporarily and rotate the other 3 in sequence.
4
Complete the Cycle
Place the saved element in its final position, completing the 4-element clockwise rotation.
Key Takeaway
๐ŸŽฏ Key Insight: By treating the matrix as concentric rings and rotating 4 elements at a time, we achieve optimal in-place rotation with just one temporary variable per cycle!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

We still need to process each of the nยฒ elements exactly once

n
2n
โš  Quadratic Growth
Space Complexity
O(1)

Only use a constant amount of extra space for temporary variables

n
2n
โœ“ Linear Space

Constraints

  • n == matrix.length == matrix[i].length
  • 1 โ‰ค n โ‰ค 20
  • -1000 โ‰ค matrix[i][j] โ‰ค 1000
  • Must be solved in-place with O(1) extra space
Asked in
Google 42 Amazon 38 Microsoft 31 Meta 28 Apple 24
52.0K Views
High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen