Spiral Fill
Build a grid with rows rows and cols columns and write the numbers 1 to rows * cols into it along a spiral that turns counterclockwise: put 1 in the top-left cell, continue down the first column, then right along the bottom row, then up the last column, then left along the top row, and keep turning inward in the same way until every cell is filled. Return the grid.
Example 1
rows 3
cols 4
returns [[1,10,9,8],[2,11,12,7],[3,4,5,6]]
1, 2, 3 go down the first column, 4, 5, 6 along the bottom, 7, 8 up the right side, 9, 10 back along the top, then 11 and 12 fill the inside.
Example 2
rows 2
cols 2
returns [[1,4],[2,3]]
Down, right, up: 1 and 2 in the first column, 3 bottom-right, 4 top-right.
Constraints
1 <= rows, cols <= 10
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Grids problems
- Spiral OrderMedium
- Spiral Out From a CellMedium
- Sudoku Rules CheckMedium
- Turn Every RingMedium
- Zigzag DiagonalsMedium
- Darken the BoardHard
- Flags in OrderHard
- Island ShapesHard