Spiral Matrix Leetcode in Python

Sun Jul 21 2024

Spiral Matrix Leetcode

The spiral matrix leetcode problem is a popular coding challenge that involves traversing a matrix in a spiral order. This problem tests your ability to manipulate arrays and understand different traversal techniques. It's frequently asked in technical interviews and helps in honing your problem-solving skills.

Problem statement

Given a matrix of m x n elements, return all elements of the matrix in spiral order.

Example

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

Idea to solve

To solve the spiral matrix leetcode problem, you need to simulate the process of going around the matrix layer by layer. Start from the outermost layer and move inward, collecting elements in the required order.

Detailed explanation

  • Initialize Boundaries: Set the initial boundaries for top, bottom, left, and right.
  • Traverse the Matrix:
    • Move from left to right across the top row, then move the top boundary down.
    • Move from top to bottom along the right column, then move the right boundary left.
    • Move from right to left across the bottom row, then move the bottom boundary up.
    • Move from bottom to top along the left column, then move the left boundary right.
  • Repeat until all elements are collected.

Full code in Python

Here's the complete Python code to solve the spiral matrix leetcode problem:

python
1def spiralOrder(matrix):
2    if not matrix:
3        return []
4
5    result = []
6    top, bottom = 0, len(matrix) - 1
7    left, right = 0, len(matrix[0]) - 1
8
9    while top <= bottom and left <= right:
10        # Traverse from left to right
11        for i in range(left, right + 1):
12            result.append(matrix[top][i])
13        top += 1
14
15        # Traverse from top to bottom
16        for i in range(top, bottom + 1):
17            result.append(matrix[i][right])
18        right -= 1
19
20        if top <= bottom:
21            # Traverse from right to left
22            for i in range(right, left - 1, -1):
23                result.append(matrix[bottom][i])
24            bottom -= 1
25
26        if left <= right:
27            # Traverse from bottom to top
28            for i in range(bottom, top - 1, -1):
29                result.append(matrix[i][left])
30            left += 1
31
32    return result
33
34# Example usage
35matrix = [
36    [1, 2, 3],
37    [4, 5, 6],
38    [7, 8, 9]
39]
40print("Spiral order of the matrix:", spiralOrder(matrix))

Explanation of the code

  • Initialize Boundaries: Set the initial boundaries for top, bottom, left, and right.
  • Traverse the Matrix: Use a while loop to traverse the matrix in a spiral order by updating the boundaries and collecting elements in the result list.
  • Return Result: After traversing the entire matrix, return the result list containing the elements in spiral order.

FAQs

What is a spiral matrix?

How to traverse a spiral matrix?

How do you fill a matrix in spiral form?

What is the time complexity of a spiral matrix?

Sign-in First to Add Comment

Leave a comment 💬

All Comments

No comments yet.