Skip to content

Commit 45fade4

Browse files
committed
New Question added
1 parent 892b59e commit 45fade4

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Spiral Order Traversal of a Matrix
2+
3+
## Introduction
4+
5+
This repository contains a C++ solution to the problem of traversing a matrix in a spiral order. Given a 2D matrix, the objective is to traverse its elements in a spiral pattern, starting from the top-left corner and moving clockwise until all elements are visited. This algorithm is implemented using a straightforward iterative approach.
6+
7+
## Approach
8+
9+
1. **Initialize Variables**:
10+
11+
- Begin by initializing an empty vector `ans` to store the elements of the matrix in spiral order.
12+
- Obtain the dimensions of the input matrix: the number of rows `m` and the number of columns `n`.
13+
- Calculate the total number of elements `total_elements` in the matrix as the product of `m` and `n`.
14+
15+
2. **Define Boundaries**:
16+
17+
- Define four variables to track the boundaries of the traversal: `startingRow`, `startingCol`, `endingRow`, and `endingCol`.
18+
- Initially, set `startingRow` and `startingCol` to 0, representing the starting row and column indices of the traversal.
19+
- Set `endingRow` to `m-1` and `endingCol` to `n-1`, representing the ending row and column indices of the traversal respectively.
20+
21+
3. **Traverse in Spiral Order**:
22+
23+
- Utilize a `while` loop to continue traversing until all elements are visited.
24+
- Inside the loop, traverse the four sides of the matrix in clockwise order:
25+
- Traverse the top row from `startingCol` to `endingCol`, adding each element to `ans`.
26+
- Increment `startingRow` to move to the next row.
27+
- Traverse the rightmost column from `startingRow` to `endingRow`, adding each element to `ans`.
28+
- Decrement `endingCol` to move to the previous column.
29+
- Traverse the bottom row from `endingCol` to `startingCol`, adding each element to `ans`.
30+
- Decrement `endingRow` to move to the previous row.
31+
- Traverse the leftmost column from `endingRow` to `startingRow`, adding each element to `ans`.
32+
- Increment `startingCol` to move to the next column.
33+
- Increment the `count` variable after adding each element to `ans`.
34+
35+
4. **Return Result**:
36+
- Finally, return the vector `ans` containing the spiral order traversal of the matrix.
37+
38+
#
39+
40+
# Time and Space Complexity Analysis
41+
42+
## Time Complexity
43+
44+
- **Traversal**: The algorithm traverses each element of the matrix exactly once in a clockwise spiral order.
45+
- The number of elements in the matrix is `m x n`.
46+
- Therefore, the time complexity of the traversal is O(m x n).
47+
48+
## Space Complexity
49+
50+
- **Output Space**: The space complexity is primarily determined by the space required to store the output vector `ans`.
51+
- The size of the output vector `ans` is equal to the number of elements in the matrix, which is `m x n`.
52+
- Therefore, the space complexity for storing the output vector is O(m x n).
53+
- **Additional Space**: The algorithm uses a constant amount of additional space for variables like `startingRow`, `startingCol`, `endingRow`, `endingCol`, `count`, and loop variables.
54+
- Hence, the additional space complexity is O(1).
55+
56+
### Summary
57+
58+
- **Time Complexity**: O(m x n)
59+
- **Space Complexity**: O(m x n)
60+
61+
The time complexity is determined by the number of elements in the matrix, as the algorithm visits each element once. The space complexity is mainly influenced by the size of the output vector, which stores the elements of the matrix in spiral order. Overall, the algorithm has a linear time complexity relative to the input size and a space complexity proportional to the size of the matrix.
62+
63+
## Conclusion
64+
65+
This approach ensures that all elements of the matrix are traversed exactly once in a spiral order, starting from the top-left corner and moving clockwise until all elements are visited. The implementation is concise and efficient, providing a solution to the spiral order traversal problem.

Spiral-Matrix/Question.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<h1> <a href = "https://leetcode.com/problems/spiral-matrix/description/" target = "_blank" >54. Spiral Matrix</a></h1>
2+
3+
<h3>Given an m x n matrix, return all elements of the matrix in spiral order.</h3>
4+
5+
Example1:
6+
7+
![alt text](image.png)
8+
9+
<strong>Input:</strong> matrix = [[1,2,3],[4,5,6],[7,8,9]]
10+
<br/>
11+
<strong>Output:</strong> [1,2,3,6,9,8,7,4,5]
12+
13+
Example2:
14+
15+
![alt text](image-1.png)
16+
17+
<strong>Input: </strong>matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
18+
<br/>
19+
20+
<strong>Output:</strong> [1,2,3,4,8,12,11,10,9,5,6,7]
21+
22+
<strong>Constraints:</strong>
23+
24+
<ul>
25+
<li>m == matrix.length</li>
26+
<li>n == matrix[i].length </li>
27+
<li>1 <= m, n <= 10</li>
28+
<li>-100 <= matrix[i][j] <= 100</li>
29+
</ul>

Spiral-Matrix/image-1.png

20.1 KB
Loading

Spiral-Matrix/image.png

15.1 KB
Loading

Spiral-Matrix/solution.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
class Solution
2+
{
3+
public:
4+
vector<int> spiralOrder(vector<vector<int>> &matrix)
5+
{
6+
7+
vector<int> ans;
8+
int m = matrix.size();
9+
int n = matrix[0].size();
10+
int total_elements = m * n;
11+
12+
int startingRow = 0;
13+
int startingCol = 0;
14+
int endingRow = (m - 1);
15+
int endingCol = (n - 1);
16+
17+
int count = 0;
18+
19+
while (count < total_elements)
20+
{
21+
22+
// print startingRow:
23+
for (int i = startingCol; i <= endingCol && count < total_elements; i++)
24+
{
25+
ans.push_back(matrix[startingRow][i]);
26+
count++;
27+
}
28+
29+
startingRow++;
30+
31+
// print endingCol:
32+
for (int i = startingRow; i <= endingRow && count < total_elements; i++)
33+
{
34+
ans.push_back(matrix[i][endingCol]);
35+
count++;
36+
}
37+
endingCol--;
38+
39+
// print endingRow:
40+
for (int i = endingCol; i >= startingCol && count < total_elements; i--)
41+
{
42+
ans.push_back(matrix[endingRow][i]);
43+
count++;
44+
}
45+
endingRow--;
46+
47+
// print startingCol:
48+
49+
for (int i = endingRow; i >= startingRow && count < total_elements; i--)
50+
{
51+
ans.push_back(matrix[i][startingCol]);
52+
count++;
53+
}
54+
startingCol++;
55+
}
56+
return ans;
57+
}
58+
};

0 commit comments

Comments
 (0)