Matrix Rotate - FreeCodeCamp #27 Daily Challenge
2 min
Matrix Rotate - FreeCodeCamp #27
🚩 Problem Statement
Given a matrix (array of arrays), rotate it 90° clockwise. Example: for [[1,2],[3,4]], the output should be [[3,1],[4,2]].
1. Initial Analysis
What does it mean to rotate a matrix 90°? Each row of the original becomes a column in the new one, but in reverse order. Visualize it like this:
flowchart TD A["[1,2]"] --> C["[3,1]"] B["[3,4]"] --> D["[4,2]"]
Key Test Cases
- 1x1: Input:
[[1]]→ Output:[[1]] - 2x2: Input:
[[1,2],[3,4]]→ Output:[[3,1],[4,2]] - 3x3: Input:
[[1,2,3],[4,5,6],[7,8,9]]→ Output:[[7,4,1],[8,5,2],[9,6,3]] - With zeros: Input:
[[0,1,0],[1,0,1],[0,0,0]]→ Output:[[0,1,0],[0,0,1],[0,1,0]}
2. Strategy & Step by Step
How do we solve it? The clearest way is to create a new empty matrix and map each element to its new position. The trick is in the indices:
- The element at
(i, j)in the original goes to(j, n-1-i)in the rotated, wherenis the number of rows.
Algorithm:
- Get dimensions: rows
n, columnsm. - Create an empty matrix of size
m x n. - For each element
(i, j), place it at(j, n-1-i). - Return the rotated matrix.
3. JavaScript Implementation
function rotate(matrix) {
if (!matrix.length || !matrix[0].length)
return []
const n = matrix.length
const m = matrix[0].length
const rotated = Array.from({ length: m }, () => new Array(n))
for (let i = 0; i < n; i++) {
for (let j = 0; j < m; j++) {
rotated[j][n - 1 - i] = matrix[i][j]
}
}
return rotated
}4. Complexity & Edge Cases
Complexity
- Time: (we visit every element)
- Space: (new matrix)
Edge Cases
- Empty matrix:
[]→[] - Empty rows:
[[ ]]→[] - 1x1: remains the same
- Non-square: works for any shape
5. Reflections & Learnings
What did we learn?
- Index manipulation and 2D arrays
- Nested loops and position mapping
- Input validation and edge cases
- Complexity analysis
Can it be optimized?
If the matrix is square and you can modify it, you can rotate in-place: first transpose, then reverse each row. But for rectangular matrices or if you want to preserve the original, the presented method is the safest and clearest.
6. Resources
Ready to try with a bigger matrix? What if the matrix is very rectangular? 🤔