Top Interview 150
The Spiral Matrix problem is a common challenge involving matrix traversal. Letβs break down LeetCode 54: Spiral Matrix and implement a solution that works for any mΓn matrix.
π Problem Description
Given an mΓn matrix, traverse it in spiral order and return the elements in a single array.
π‘ Examples
Input: matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Output: [1, 2, 3, 6, 9, 8, 7, 4, 5]
Input: matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
Output: [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7]
Constraints
- 1β€m,nβ€10
- β100β€matrix[i][j]β€100
π JavaScript Solution
We solve this problem by defining four boundaries (top
, bottom
, left
, and right
) that shrink as we traverse the matrix in spiral order.
Implementation
var spiralOrder = function(matrix) {
const result = [];
let top = 0;
let bottom = matrix.length - 1;
let left = 0;
let right = matrix[0].length - 1;
while (top <= bottom && left <= right) {
for (let i = left; i <= right; i++) {
result.push(matrix[top][i]);
}
top++;
for (let i = top; i <= bottom; i++) {
result.push(matrix[i][right]);
}
right--;
if (top <= bottom) {
for (let i = right; i >= left; i--) {
result.push(matrix[bottom][i]);
}
bottom--;
}
if (left <= right) {
for (let i = bottom; i >= top; i--) {
result.push(matrix[i][left]);
}
left++;
}
}
return result;
};
π How It Works
-
Define Boundaries:
-
top
: Tracks the topmost row. -
bottom
: Tracks the bottommost row. -
left
: Tracks the leftmost column. -
right
: Tracks the rightmost column.
-
-
Traverse in Spiral Order:
- Left to Right: Traverse the top row, then increment
top
. - Top to Bottom: Traverse the rightmost column, then decrement
right
. - Right to Left: Traverse the bottom row (if it exists), then decrement
bottom
. - Bottom to Top: Traverse the leftmost column (if it exists), then increment
left
.
- Left to Right: Traverse the top row, then increment
-
Check for Overlap:
- Ensure the boundaries (
top
,bottom
,left
,right
) do not overlap before processing each direction.
- Ensure the boundaries (
π Complexity Analysis
-
Time Complexity:
O(mβ n)
, wherem
is the number of rows andn
is the number of columns.- Each element is processed once.
Space Complexity:
O(1)
, excluding the space required for the output array.
π Dry Run
Input: matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Output: [1, 2, 3, 6, 9, 8, 7, 4, 5]
β¨ Pro Tips for Interviews
-
Clarify Constraints:
- Ask if the input matrix can be empty.
- Confirm if non-rectangular matrices are possible (unlikely, but good to clarify).
-
Discuss Edge Cases:
- Single-row matrix:
[[1, 2, 3]]
. - Single-column matrix:
[[1], [2], [3]]
. - Smallest possible matrix:
[[1]]
.
- Single-row matrix:
-
Highlight Efficiency:
- Explain why the shrinking boundary approach is
O(mβ n)
.
- Explain why the shrinking boundary approach is
π Learn More
Check out the full explanation and code walkthrough on my previous Dev.to post:
π Valid Sudoku - JavaScript Solution
Whatβs your preferred method to solve this problem? Letβs discuss! π
Top comments (1)
Follow Me on GitHub π
If you found this solution helpful, check out more of my projects and solutions on my GitHub profile.
Don't forget to follow for more updates!