From 7ed3aa0d8192a84f8a2d65b4adc75317ec393dc3 Mon Sep 17 00:00:00 2001 From: Nishant Kaushal <101548649+nishant0708@users.noreply.github.com> Date: Tue, 11 Jun 2024 00:12:44 +0530 Subject: [PATCH 1/3] added q54 --- .../0000-0099/0054-spiral-matrix.md | 242 ++++++++++++++++++ 1 file changed, 242 insertions(+) create mode 100644 dsa-solutions/lc-solutions/0000-0099/0054-spiral-matrix.md diff --git a/dsa-solutions/lc-solutions/0000-0099/0054-spiral-matrix.md b/dsa-solutions/lc-solutions/0000-0099/0054-spiral-matrix.md new file mode 100644 index 000000000..e628f25f7 --- /dev/null +++ b/dsa-solutions/lc-solutions/0000-0099/0054-spiral-matrix.md @@ -0,0 +1,242 @@ +--- +id: spiral-matrix +title: Spiral Matrix +sidebar_label: 0054 Spiral Matrix +tags: +- Array +- Matrix +- Simulation +- C++ +- Java +- Python +description: "This document provides a solution to the Spiral Matrix problem, where the goal is to traverse a matrix in spiral order." +--- + +## Problem + +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` + +## Solution + +To solve this problem, we need to simulate the traversal of the matrix in spiral order. We can define four boundaries to keep track of the rows and columns that we have already traversed: `top`, `bottom`, `left`, and `right`. We start at the top-left corner and move in the following order: + +1. Traverse from left to right. +2. Traverse downwards. +3. Traverse from right to left. +4. Traverse upwards. + +We continue this process while adjusting the boundaries until all elements have been traversed. + +### Step-by-Step Approach + +1. Initialize `top`, `bottom`, `left`, and `right` boundaries. +2. Use a loop to traverse the matrix until all elements are visited: + - Traverse from left to right within the `top` boundary and increment `top`. + - Traverse downwards within the `right` boundary and decrement `right`. + - Traverse from right to left within the `bottom` boundary and decrement `bottom`. + - Traverse upwards within the `left` boundary and increment `left`. + +### Code in Different Languages + + + + + +```cpp + +#include +using namespace std; + +vector spiralOrder(vector>& matrix) { + if (matrix.empty()) return {}; + + int m = matrix.size(), n = matrix[0].size(); + vector result; + int top = 0, bottom = m - 1; + int left = 0, right = n - 1; + + while (top <= bottom && left <= right) { + // Traverse from left to right + for (int i = left; i <= right; ++i) { + result.push_back(matrix[top][i]); + } + ++top; + + // Traverse downwards + for (int i = top; i <= bottom; ++i) { + result.push_back(matrix[i][right]); + } + --right; + + if (top <= bottom) { + // Traverse from right to left + for (int i = right; i >= left; --i) { + result.push_back(matrix[bottom][i]); + } + --bottom; + } + + if (left <= right) { + // Traverse upwards + for (int i = bottom; i >= top; --i) { + result.push_back(matrix[i][left]); + } + ++left; + } + } + + return result; +} + +int main() { + vector> matrix = { + {1, 2, 3, 4}, + {5, 6, 7, 8}, + {9, 10, 11, 12} + }; + vector result = spiralOrder(matrix); + for (int num : result) { + cout << num << " "; + } + return 0; +} + + + + import java.util.ArrayList; +import java.util.List; + +public class SpiralMatrix { + public List spiralOrder(int[][] matrix) { + List result = new ArrayList<>(); + if (matrix.length == 0) return result; + + int top = 0, bottom = matrix.length - 1; + int left = 0, right = matrix[0].length - 1; + + while (top <= bottom && left <= right) { + for (int i = left; i <= right; i++) { + result.add(matrix[top][i]); + } + top++; + + for (int i = top; i <= bottom; i++) { + result.add(matrix[i][right]); + } + right--; + + if (top <= bottom) { + for (int i = right; i >= left; i--) { + result.add(matrix[bottom][i]); + } + bottom--; + } + + if (left <= right) { + for (int i = bottom; i >= top; i--) { + result.add(matrix[i][left]); + } + left++; + } + } + + return result; + } + + public static void main(String[] args) { + SpiralMatrix sm = new SpiralMatrix(); + int[][] matrix = { + {1, 2, 3, 4}, + {5, 6, 7, 8}, + {9, 10, 11, 12} + }; + System.out.println(sm.spiralOrder(matrix)); // Output: [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7] + } +} + + + + def spiralOrder(matrix): + if not matrix: + return [] + + result = [] + top, bottom = 0, len(matrix) - 1 + left, right = 0, len(matrix[0]) - 1 + + while top <= bottom and left <= right: + # Traverse from left to right + for i in range(left, right + 1): + result.append(matrix[top][i]) + top += 1 + + # Traverse downwards + for i in range(top, bottom + 1): + result.append(matrix[i][right]) + right -= 1 + + if top <= bottom: + # Traverse from right to left + for i in range(right, left - 1, -1): + result.append(matrix[bottom][i]) + bottom -= 1 + + if left <= right: + # Traverse upwards + for i in range(bottom, top - 1, -1): + result.append(matrix[i][left]) + left += 1 + + return result + +matrix = [ + [1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12] +] +print(spiralOrder(matrix)) # Output: [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7] + + + +Complexity Analysis +Time Complexity: O(m * n) +Reason: We visit every element in the matrix exactly once. + +Space Complexity: O(1) +Reason: We only use a fixed amount of extra space, regardless of the input size. +References +LeetCode Problem: Spiral Matrix + +Author's LeetCode Profile: Vipul Lakum +``` From 916de291e047295e57755db57478b3b24897479d Mon Sep 17 00:00:00 2001 From: Nishant Kaushal <101548649+nishant0708@users.noreply.github.com> Date: Tue, 11 Jun 2024 02:53:22 +0530 Subject: [PATCH 2/3] updated --- .../0000-0099/0054-spiral-matrix.md | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/dsa-solutions/lc-solutions/0000-0099/0054-spiral-matrix.md b/dsa-solutions/lc-solutions/0000-0099/0054-spiral-matrix.md index e628f25f7..4d08d068e 100644 --- a/dsa-solutions/lc-solutions/0000-0099/0054-spiral-matrix.md +++ b/dsa-solutions/lc-solutions/0000-0099/0054-spiral-matrix.md @@ -132,9 +132,10 @@ int main() { return 0; } - - - import java.util.ArrayList; +``` +### JAVA +``` +import java.util.ArrayList; import java.util.List; public class SpiralMatrix { @@ -185,8 +186,10 @@ public class SpiralMatrix { } } - - +``` +### Python + +``` def spiralOrder(matrix): if not matrix: return [] @@ -228,15 +231,19 @@ matrix = [ print(spiralOrder(matrix)) # Output: [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7] +``` -Complexity Analysis -Time Complexity: O(m * n) -Reason: We visit every element in the matrix exactly once. -Space Complexity: O(1) -Reason: We only use a fixed amount of extra space, regardless of the input size. -References -LeetCode Problem: Spiral Matrix +### Complexity Analysis +#### Time Complexity: O(m * n) +**Reason:** We visit every element in the matrix exactly once. + +**Space Complexity:** O(1) +**Reason:** We only use a fixed amount of extra space, regardless of the input size. +### References +**LeetCode Problem:** Spiral Matrix -Author's LeetCode Profile: Vipul Lakum +**Author's LeetCode Profile:** Vipul Lakum ``` + + From c131d5ef5257450e214766d450fac88f1afada81 Mon Sep 17 00:00:00 2001 From: Nishant Kaushal <101548649+nishant0708@users.noreply.github.com> Date: Tue, 11 Jun 2024 22:26:33 +0530 Subject: [PATCH 3/3] updated --- .../0000-0099/0054-spiral-matrix.md | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/dsa-solutions/lc-solutions/0000-0099/0054-spiral-matrix.md b/dsa-solutions/lc-solutions/0000-0099/0054-spiral-matrix.md index 4d08d068e..ba90eb387 100644 --- a/dsa-solutions/lc-solutions/0000-0099/0054-spiral-matrix.md +++ b/dsa-solutions/lc-solutions/0000-0099/0054-spiral-matrix.md @@ -1,7 +1,7 @@ --- id: spiral-matrix title: Spiral Matrix -sidebar_label: 0054 Spiral Matrix +sidebar_label: 0054-Spiral-Matrix tags: - Array - Matrix @@ -69,9 +69,7 @@ We continue this process while adjusting the boundaries until all elements have ### Code in Different Languages - - - +### C++ Solution ```cpp @@ -133,8 +131,8 @@ int main() { } ``` -### JAVA -``` +### JAVA Solution +```java import java.util.ArrayList; import java.util.List; @@ -185,11 +183,11 @@ public class SpiralMatrix { System.out.println(sm.spiralOrder(matrix)); // Output: [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7] } } - + ``` -### Python +### Python Solution -``` +```python def spiralOrder(matrix): if not matrix: return [] @@ -230,20 +228,15 @@ matrix = [ ] print(spiralOrder(matrix)) # Output: [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7] - ``` - ### Complexity Analysis #### Time Complexity: O(m * n) -**Reason:** We visit every element in the matrix exactly once. +>Reason: We visit every element in the matrix exactly once. **Space Complexity:** O(1) -**Reason:** We only use a fixed amount of extra space, regardless of the input size. +>Reason: We only use a fixed amount of extra space, regardless of the input size. ### References **LeetCode Problem:** Spiral Matrix -**Author's LeetCode Profile:** Vipul Lakum -``` -