#19Garden Path
A rectangular garden is divided into rows and columns of plants. Each section of the garden contains an integer representing the plant type in that section.
A gardener starts at the top-left section of the garden and walks around the garden in a clockwise spiral path. After reaching a boundary, the gardener turns and continues inward until every section has been visited.
Return the values of the plants in the exact order in which the gardener visits them.
Examples
Example 1
Input: matrix = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]]
Output: [1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10]
Explanation: The gardener follows the path: → → → ↓ ↓ ↓ ← ← ← ↑ ↑ → → ↓ ← The outer boundary is visited first, followed by the inner sections.
Example 2
Input: matrix = [[7, 8, 9],
[4, 5, 6]]
Output: [7, 8, 9, 6, 5, 4]
Explanation: The gardener first walks across the top row, then down the right side, and finally across the bottom row in reverse.
Example 3
Input: matrix = [[1],
[2],
[3],
[4],
[5]]
Output: [1, 2, 3, 4, 5]
Explanation: Since the garden has only one column, the gardener simply moves downward.
Constraints
- 1 ≤ m, n ≤ 1000
- m × n ≤ 2 × 10⁵
- -10⁹ ≤ matrix[i][j] ≤ 10⁹
- Every row contains exactly n elements.
- The garden contains at least one section.
