/59. Spiral Matrix Generation

59. Spiral Matrix Generation

Medium
Arrays74.7% acceptance

Given an integer dimension_size, generate a square matrix of size dimension_size x dimension_size. Fill the matrix with consecutive integers from 1 to dimension_size^2 in spiral order, starting from the top-left cell and proceeding clockwise.

Example 1

Input: 2

Output: [[1, 2], [4, 3]]

Explanation: The matrix is filled in spiral order: 1, 2, 3, 4.

Example 2

Input: 4

Output: [[1, 2, 3, 4], [12, 13, 14, 5], [11, 16, 15, 6], [10, 9, 8, 7]]

Explanation: The matrix is filled in spiral order from 1 to 16.

Constraints

  • 1 <= dimension_size <= 20
Python (current runtime)

Case 1

Input: 5

Expected: [[1, 2, 3, 4, 5], [16, 17, 18, 19, 6], [15, 24, 25, 20, 7], [14, 23, 22, 21, 8], [13, 12, 11, 10, 9]]

Case 2

Input: 3

Expected: [[1, 2, 3], [8, 9, 4], [7, 6, 5]]