MEDIUM NC#136 Blind #48 Math & Geometry

48. Rotate Image

šŸ“– Problem

You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise). You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

🧠 Visual Learning Aid

1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases

JS/TS Refreshers

  • •Array methods (`push`, `pop`, `shift`, `slice`)
  • •Object/Map/Set usage patterns
  • •Function parameter and return typing

Logical Thinking Concepts

  • •Define invariants before coding
  • •Check edge cases first (`[]`, single element, duplicates)
  • •Estimate time/space before implementation

šŸ’” Approach

  • → Transpose the matrix (swap rows and columns)
  • → Reverse each row
  • → Combined effect is 90-degree clockwise rotation
  • → Time: O(n²), Space: O(1)

🧭 Prerequisites

šŸ› ļø Hints & Pitfalls

Hints

  • •Transpose the matrix (swap rows and columns)
  • •Reverse each row
  • •Combined effect is 90-degree clockwise rotation

Common Pitfalls

  • •Time: O(n²), Space: O(1)

🧪 Test Cases

Test Case 1
Not run
Input:
rotate(matrix1);
Expected:
null
Test Case 2
Not run
Input:
rotate(matrix2);
Expected:
null
Test Case 3
Not run
Input:
rotate(matrix: number[][]);
Expected:
Computed from hidden reference

šŸ“ Code Editor

šŸ“š Reference Solution

ā–¼
⌘K Search āŒ˜ā†© Run ⌘S Submit