Hone logo
Hone
Problems

Navigate a Matrix in Spiral Order

This challenge involves traversing a 2D array (matrix) in a specific, clockwise spiral pattern, collecting all its elements into a one-dimensional list. This type of pattern traversal is a fundamental skill in algorithm design and can be applied in various scenarios like processing images, mapping game levels, or visualizing data streams.

Problem Description

Your task is to implement a function that takes an m x n matrix as input and returns a new list containing all elements of the matrix in spiral order. The spiral traversal should start from the top-left element, move right across the first row, then down the last column, then left across the last row, then up the first column, and continue inwards in the same clockwise fashion until all elements have been visited exactly once.

Here's a breakdown of the expected behavior:

  • Initialization: Begin at the top-left corner (0, 0).
  • Rightward movement: Traverse from (row, col) to (row, col+1) until the right boundary is hit.
  • Downward movement: Traverse from (row, col) to (row+1, col) until the bottom boundary is hit.
  • Leftward movement: Traverse from (row, col) to (row, col-1) until the left boundary is hit.
  • Upward movement: Traverse from (row, col) to (row-1, col) until the top boundary is hit.
  • Boundary Shrinkage: After completing each full "layer" of the spiral, the effective boundaries of the matrix should shrink inwards to process the next inner layer.
  • Termination: The traversal should continue until all elements have been added to the result list. Be careful to avoid double-counting elements, especially in rectangular matrices or when m or n is odd.

Examples

Example 1:

Input: [[1,2,3],[4,5,6],[7,8,9]]
Output: [1,2,3,6,9,8,7,4,5]
Explanation: Start at 1, go right (1,2,3), down (6,9), left (8,7), up (4), then right again (5).

Example 2:

Input: [[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]
Explanation: Start at 1, go right (1,2,3,4), down (8,12), left (11,10,9), up (5), then right again (6,7).

Example 3:

Input: [[1],[2],[3],[4]]
Output: [1,2,3,4]
Explanation: A single column matrix. Traverse straight down.

Constraints

  • m is the number of rows, n is the number of columns.
  • 1 <= m, n <= 100
  • Matrix elements matrix[i][j] are integers.
  • -100 <= matrix[i][j] <= 100
  • The expected time complexity is O(m * n) since every element must be visited once.
  • The expected auxiliary space complexity (excluding the output list) is O(1).

Notes

  • Consider using four pointers to keep track of the current boundaries of the sub-matrix you are processing: top_row, bottom_row, left_col, right_col.
  • After traversing each side (right, down, left, up), remember to update the corresponding boundary pointer to shrink the processing area. For example, after traversing the top row, increment top_row.
  • Pay close attention to the conditions for when to stop the traversal or when a particular direction's traversal should not happen (e.g., if top_row > bottom_row or left_col > right_col). This is crucial for handling single-row/column matrices and avoiding out-of-bounds access or duplicate elements.
  • The total number of elements in the output list should always be m * n.
Loading editor...
plaintext