> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Spiral Matrix III Python Solution with Tests

> Tested Python solution for LeetCode 885 with 12 pytest cases. Generate a practice environment with lcpy.

LeetCode 885, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Matrix](/catalog/topics/matrix), [Simulation](/catalog/topics/simulation). [View on LeetCode](https://leetcode.com/problems/spiral-matrix-iii/description/).

Generate this problem as a practice environment: tested reference solution, 12 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 885   # by problem number
lcpy gen -s spiral_matrix_iii   # by problem name
```

## Problem

You start at the cell \<code>(rStart, cStart)\</code> of an \<code>rows x cols\</code> 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.\</p>

\<p>You will walk in a clockwise spiral shape to visit every position in this grid. Whenever you move outside the grid's boundary, we continue our walk outside the grid (but may return to the grid boundary later.). Eventually, we reach all \<code>rows \* cols\</code> spaces of the grid.\</p>

\<p>Return an array of coordinates representing the positions of the grid in the order you visited them.\</p>

### Examples

![Example 1](https://assets.leetcode.com/uploads/2018/08/24/example_1.png)

```
Input: rows = 5, cols = 6, rStart = 1, cStart = 4
Output: [[1,4],[1,5],[2,5],[2,4],[2,3],[1,3],[0,3],[0,4],[0,5],[3,5],[3,4],[3,3],[3,2],[2,2],[1,2],[0,2],[4,5],[4,4],[4,3],[4,2],[4,1],[3,1],[2,1],[1,1],[0,1],[4,0],[3,0],[2,0],[1,0],[0,0]]
```

![Example 2](https://assets.leetcode.com/uploads/2018/08/24/example_2.png)

```
Input: rows = 1, cols = 4, rStart = 0, cStart = 0
Output: [[0,0],[0,1],[0,2],[0,3]]
```

### Constraints

* 1 \<= rows, cols \<= 100
* 0 \<= rStart \< rows
* 0 \<= cStart \< cols

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/spiral_matrix_iii/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/spiral_matrix_iii/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(max(rows, cols)^2)
    # Space: O(1) excluding output
    def spiral_matrix_iii(
        self, rows: int, cols: int, r_start: int, c_start: int
    ) -> list[list[int]]:
        result = [[r_start, c_start]]
        r, c, steps, direction = r_start, c_start, 1, 0
        directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
        while len(result) < rows * cols:
            for _ in range(2):
                dr, dc = directions[direction]
                for _ in range(steps):
                    r, c = r + dr, c + dc
                    if 0 <= r < rows and 0 <= c < cols:
                        result.append([r, c])
                direction = (direction + 1) % 4
            steps += 1
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(max(rows, cols)^2) | O(1) excluding output |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.