Blind 75 · Math & Geometry

Spiral Matrix

Medium

Watch the walkthrough (5:49)

Problem

Given an m x n matrix, return all its values in clockwise spiral order: across the top row, down the right edge, back across the bottom, up the left edge, then inward and around again until every value is visited once.

Examples

Example 1:
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [1,2,3,6,9,8,7,4,5]

Example 2:
Input: matrix = [[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]

Example 3:
Input: matrix = [[7]]
Output: [7]

Example 4:
Input: matrix = [[1],[2],[3]]
Output: [1,2,3]

Constraints

1 <= m, n <= 10
-100 <= matrix[i][j] <= 100

Prerequisites

Index arithmetic on a grid, and the discipline of writing the coordinate rule down before coding it.

How to think about it

1. Walk Each Edge, Then Pull It In Optimal

Intuition

Keep four edges: the top and bottom rows, and the left and right columns, starting on the grid's outer rows and columns. Each pass walks one edge clockwise and then pulls that edge in a step: the top row left to right, then top moves down; the right column downward, then right moves in; the bottom row right to left, then bottom moves up; the left column upward, then left moves in. Each edge moves in straight after its pass, so no corner is read twice. The loop runs while neither pair of opposite edges has crossed, but they can cross inside a lap: after the top pass over a single row, top is below bottom, and the bottom pass would read the same row back. So the bottom pass runs only if top <= bottom, and the left pass only if left <= right. Without those two checks, the walkthrough's single row [[2,3,4,5]] would come back as 2,3,4,5,4,3,2, three values read twice. Walk the edges clockwise, pull each edge in after walking it, and stop as soon as opposite edges cross.

Algorithm

1. Start the four edges on the grid's outer rows and columns.
2. Walk the top row left to right, then move top down; walk the right column downward, then move right in.
3. If top <= bottom, walk the bottom row right to left, then move bottom up.
4. If left <= right, walk the left column upward, then move left in.
5. Repeat while top <= bottom and left <= right, then return the values in the order read.

Time & Space

Time O(m x n): each value is read once. Space O(1) beyond the output: only the four edges are kept.

2. Four Shrinking Boundaries Optimal

Intuition

Hold four edges — top, bottom, left, right — and consume one at a time, pulling the boundary in behind you. The two guards before the bottom and left passes are what stop a single remaining row or column being read twice: after the top row is consumed, top may have passed bottom, and the bottom pass would replay it. [[1],[2],[3]] is in the tests for exactly that.

Algorithm

1. Walk the top row left to right; drop the top boundary.
2. Walk the right column downward; drop the right boundary.
3. If rows remain, walk the bottom row right to left; raise the bottom.
4. If columns remain, walk the left column upward; raise the left.
5. Repeat while the boundaries have not crossed.

Time & Space

Time O(m x n). Space O(1) beyond the output.