/59. Spiral Matrix Generation

59. Spiral Matrix Generation

Medium
Arrays74.7% acceptance

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

Example 1

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 starting from 1 up to 16.

Example 2

Input: 2

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

Explanation: The matrix is filled in spiral order starting from 1 up to 4.

Constraints

  • 1 <= matrix_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]]