Skip to content

Commit 979e29d

Browse files
feat: add Rotate Image explanation
- Transpose + reverse algorithm approach - Comprehensive problem analysis - Multiple approaches and optimizations - Edge cases and applications
1 parent 52fd0a9 commit 979e29d

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

neetcode-150/math-geometry/rotate-image/explanation.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,52 +17,52 @@ Output: [[7,4,1],[8,5,2],[9,6,3]]
1717
## Approach
1818

1919
### Method 1: Transpose + Reverse (Recommended)
20-
1. Transpose the matrix (swap rows and columns)
20+
1. Transpose the matrix (swap matrix[i][j] with matrix[j][i])
2121
2. Reverse each row
2222
3. Most efficient approach
2323

2424
**Time Complexity:** O(n²) - Two passes
2525
**Space Complexity:** O(1) - In-place modification
2626

27-
### Method 2: Layer by Layer Rotation
27+
### Method 2: Layer by Layer
2828
1. Rotate matrix layer by layer
2929
2. Use four-way swap for each layer
3030
3. Less efficient than transpose approach
3131

32-
**Time Complexity:** O(n²) - Two passes
32+
**Time Complexity:** O(n²) - Single pass
3333
**Space Complexity:** O(1) - In-place modification
3434

3535
## Algorithm
3636

3737
```
3838
1. Transpose matrix: matrix[i][j] = matrix[j][i]
39-
2. Reverse each row: matrix[i][j] = matrix[i][n-1-j]
39+
2. Reverse each row: reverse(matrix[i])
4040
```
4141

4242
## Key Insights
4343

44-
- **Transpose**: Swap rows and columns
44+
- **Transpose**: Swap elements across diagonal
4545
- **Reverse**: Reverse each row after transpose
46-
- **Local Optimum**: Efficient matrix transformation
47-
- **Global Optimum**: 90-degree clockwise rotation
46+
- **In-place**: No extra space needed
47+
- **Space Optimization**: Use only necessary space
4848

4949
## Alternative Approaches
5050

5151
1. **Layer by Layer**: Rotate layer by layer
5252
2. **Four-way Swap**: Use four-way swap for each element
53-
3. **Brute Force**: Create new matrix and copy
53+
3. **Mathematical**: Use mathematical transformation
5454

5555
## Edge Cases
5656

57-
- Single element: Return as is
58-
- 2x2 matrix: Handle appropriately
57+
- Single element: No change needed
58+
- 2x2 matrix: Simple swap
5959
- Large matrix: Handle efficiently
60-
- Empty matrix: Return empty matrix
60+
- Empty matrix: Handle appropriately
6161

6262
## Applications
6363

64-
- Matrix algorithms
6564
- Image processing
65+
- Matrix operations
6666
- Algorithm design patterns
6767
- Interview preparation
6868
- System design
@@ -72,4 +72,4 @@ Output: [[7,4,1],[8,5,2],[9,6,3]]
7272
- **Transpose + Reverse**: Most efficient approach
7373
- **Space Optimization**: O(1) space complexity
7474
- **Quadratic Time**: O(n²) time complexity
75-
- **No Extra Space**: Use only necessary space
75+
- **No Extra Space**: Use only necessary space

0 commit comments

Comments
 (0)