Spiral Matrix
MediumNeetCode 150Top Interview 150 Math & GeometryEpic SystemsRBCWissen TechnologyZohoPornHubeBayProblem
You get a grid of numbers with m rows and n columns.
Return all its numbers in spiral order: start at the top-left cell, go right along the top row, then down the right column, then left along the bottom row, then up the left column, and keep spiraling inward until every cell has been read once.
Constraints
- 1 ≤ m, n ≤ 10
- -100 ≤ matrix[i][j] ≤ 100
Examples: pick one to animate
Solutions
Time: Every cell is read exactly once, and the border bookkeeping is constant per row or column.
Space: Only four border indexes beyond the output list.
Keep top, bottom, left and right borders around the unread block.
Read the top row and move top down, read the right column and move right in, then the bottom
row and the left column the same way. After the top and right sides, the block may be a single row or column,
so check that rows and columns remain before reading the bottom and left sides.