Advertisement

Spiral Matrix - LeetCode 54 Solution

Spiral Matrix - Complete Solution Guide

Spiral Matrix is LeetCode problem 54, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an m x n matrix , return all elements of the matrix in spiral order . Example 1: Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [1,2,3,6,9,8,7,4,5] Example 2: Input: matrix = [[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] Constraints: m == matrix.length n == matrix[i].length 1 <= m, n <= 10 -100 <= matrix[i][j] <= 100

Detailed Explanation

The problem asks us to traverse a given m x n matrix in a spiral order, starting from the outermost elements and moving inwards layer by layer, and return a list of elements in that order. For example, given a 3x3 matrix [[1,2,3],[4,5,6],[7,8,9]], the spiral order is [1,2,3,6,9,8,7,4,5].

Solution Approach

The solution uses a boundary-based approach. It initializes `top`, `bottom`, `left`, and `right` pointers to represent the boundaries of the current spiral layer. It then iterates through the matrix in a spiral order by moving right, then down, then left, and then up. After each directional traversal, the corresponding boundary is updated to move inwards. The iteration continues until either `left > right` or `top > bottom`, indicating that all elements have been traversed.

Step-by-Step Algorithm

  1. Step 1: Initialize `top` to 0, `bottom` to `m-1`, `left` to 0, and `right` to `n-1`, where `m` is the number of rows and `n` is the number of columns in the matrix.
  2. Step 2: While `left <= right` and `top <= bottom`, perform the following steps:
  3. Step 3: Traverse from left to right along the `top` row and add the elements to the result list. Increment `top`.
  4. Step 4: Traverse from top to bottom along the `right` column and add the elements to the result list. Decrement `right`.
  5. Step 5: If `top <= bottom`, traverse from right to left along the `bottom` row and add the elements to the result list. Decrement `bottom`.
  6. Step 6: If `left <= right`, traverse from bottom to top along the `left` column and add the elements to the result list. Increment `left`.
  7. Step 7: Repeat steps 3-6 until the condition in step 2 is no longer met.
  8. Step 8: Return the result list.

Key Insights

  • Recognizing the spiral traversal can be broken down into four directional traversals: right, down, left, and up.
  • Maintaining boundaries (top, bottom, left, right) to keep track of the current layer of the spiral.
  • Updating the boundaries after each traversal to move inwards to the next layer.
  • Handling edge cases where the matrix might not be a square (m != n), and stopping the iteration when no more elements are left to traverse.

Complexity Analysis

Time Complexity: O(m*n)

Space Complexity: O(m*n)

Topics

This problem involves: Array, Matrix, Simulation.

Companies

Asked at: AMD, Accenture, Adobe, Amazon, Anduril, Apple, Bloomberg, Capital One, Cisco, CrowdStrike, DE Shaw, Darwinbox, Databricks, Deutsche Bank, Epic Systems, Goldman Sachs, IBM, Infosys, Intuit, J.P. Morgan, Lenskart, Meta, Microsoft, Netflix, Nordstrom, Nutanix, Nvidia, Oracle, PayPal, RBC, Roblox, SIG, Tekion, TikTok, Trexquant, Turing, Uber, Visa, Walmart Labs, WatchGuard, Wells Fargo, Yahoo, Zoho, eBay, tcs.