15% off all plans & roadmaps Use code
See plansLeetCode 885 · Solution & editorial
You start at the cell (rStart, cStart) of an rows x cols grid facing east. The northwest corner is at the first row and column in the grid, and the southeast corner is at the last row and column.
Spiral Matrix III is a Array/Matrix Manipulation Patterns problem, in the Spiral Traversal subpattern. The pattern covers in-place operations, hashing, prefix sums, spiral traversal, and cyclic sort
Master spiral traversal pattern for 2D matrices. Learn to traverse matrices in spiral order for interview problems.
The reference solution runs in O(rows * cols) time and O(rows * cols) space.
Spiral Matrix III has been reported in interviews at Facebook.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Spiral Matrix III is a Array/Matrix Manipulation Patterns problem, in the Spiral Traversal subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.