#212EasyMatrix· Spiral Traversal with Shrinking Borders
Spiral Seat Numbers
Input format
`grid`: a non-empty rectangular grid of integers.
Output format
An array with every value of the grid, in clockwise spiral order starting at the top-left cell.
Examples
Input: grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Output: [1, 2, 3, 6, 9, 8, 7, 4, 5]
Right along the top (1, 2, 3), down the right edge (6, 9), left along the bottom (8, 7), up the left edge (4) and finally the single middle seat 5.
Input: grid = [[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]
The first lap visits 1 2 3 4, 8 12, 11 10 9 and 5. The second lap only has the middle row left: 6 and 7.
Input: grid = [[7], [8], [9]]
Output: [7, 8, 9]
A single column: the usher walks straight down. The border logic must not visit the same seat twice on the way back up.
Constraints
- 1 <= grid.length, grid[0].length <= 100
- All rows have the same length.
- -1000 <= grid[r][c] <= 1000
- #matrix
- #simulation
- #traversal
- #arrays