1. Boundary Traversal of Matrix
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem of boundary traversal of a matrix can be found at the following link:
To perform the boundary traversal of a matrix, I followed these steps:
Initialize an empty vector out
to store the boundary elements.
Initialize variables i
and j
to track the current position in the matrix, both starting at 0.
Traverse the top row from left to right, incrementing j
while adding elements to out
.
Increment i
and decrement j
to position at the last column if there is more than one row.
If there are more than one rows and more than one columns, traverse the rightmost column from top to bottom while decrementing i
and adding elements to out
.
If there are more than one rows, return to the first column by incrementing j
and traverse the bottom row from bottom to top while decrementing i
and adding elements to out
.
Return the out
vector as the result.
Time Complexity: The time complexity of this solution is O(N + M)
, where N
is the number of rows in the matrix and M
is the number of columns in the matrix. We visit each element of the matrix once.
Auxiliary Space Complexity: The auxiliary space complexity is O(N+M)
because we only use a single vector to store the result.
For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star to the repository.