Spiral Matrix IV - Problem

Welcome to one of the most elegant matrix problems! ๐ŸŒช๏ธ You're tasked with creating a spiral matrix that combines linked list traversal with matrix manipulation.

Given:

  • Dimensions: Two integers m and n representing matrix size
  • Data source: Head of a linked list containing integers

Your mission: Generate an m ร— n matrix by placing linked list values in clockwise spiral order, starting from the top-left corner. Think of it as drawing a spiral from outside to inside!

๐ŸŽฏ Special rule: If you run out of linked list values before filling the entire matrix, fill remaining positions with -1.

Example: With a 3ร—3 matrix and linked list [3,0,2,6,8,1,7,9,4,2,5,5,0], you'd spiral clockwise: right โ†’ down โ†’ left โ†’ up โ†’ repeat until the center is reached.

Input & Output

example_1.py โ€” Basic Spiral
$ Input: m = 3, n = 5, head = [3,0,2,6,8,1,7,9,4,2,5,5,0]
โ€บ Output: [[3,0,2,6,8],[5,0,-1,-1,1],[5,2,4,9,7]]
๐Ÿ’ก Note: Starting from top-left, we spiral clockwise: first row (3โ†’0โ†’2โ†’6โ†’8), then right column (1โ†’7), bottom row (9โ†’4โ†’2โ†’5), left column (5), and finally the remaining center positions with linked list values or -1.
example_2.py โ€” Perfect Fit
$ Input: m = 1, n = 4, head = [0,1,2]
โ€บ Output: [[0,1,2,-1]]
๐Ÿ’ก Note: Single row matrix: fill left to right with linked list values [0,1,2], then pad remaining position with -1.
example_3.py โ€” Excess Values
$ Input: m = 2, n = 2, head = [1,2,3,4,5,6,7,8]
โ€บ Output: [[1,2],[4,3]]
๐Ÿ’ก Note: 2ร—2 matrix filled in spiral order: (0,0)โ†’1, (0,1)โ†’2, (1,1)โ†’3, (1,0)โ†’4. Extra linked list values (5,6,7,8) are ignored since matrix is full.

Constraints

  • 1 โ‰ค m, n โ‰ค 105
  • 1 โ‰ค m ร— n โ‰ค 105
  • The number of nodes in the list is in the range [1, m ร— n]
  • 0 โ‰ค Node.val โ‰ค 1000

Visualization

Tap to expand
302681794255-1-1-1๐ŸŒน Layer 1: Top RowPlant: 3โ†’0โ†’2โ†’6โ†’8๐ŸŒธ Layer 2: Right ColumnPlant: 1โ†’7๐ŸŒป Layer 3: Bottom RowPlant: 9โ†’4โ†’2โ†’5๐ŸŒบ Layer 4: Left ColumnPlant: 5โŒ Empty SpotsFill with -1 (no more flowers)๐ŸŒบ Spiral Garden: Clockwise Planting Pattern
Understanding the Visualization
1
Plant the Perimeter
Start at the northwest corner, plant along the north border (top row)
2
Move Down the East Side
Turn clockwise, plant down the east border (right column)
3
Cross the South Border
Turn clockwise again, plant across the south border (bottom row, right to left)
4
Up the West Side
Final turn, plant up the west border (left column, bottom to top)
5
Spiral Inward
Repeat the pattern for inner layers until the garden center is reached or flowers run out
Key Takeaway
๐ŸŽฏ Key Insight: By maintaining shrinking boundaries instead of checking individual positions, we create an elegant and efficient spiral traversal that mirrors natural growth patterns.
Asked in
Google 85 Meta 72 Amazon 68 Microsoft 45
25.0K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen