Open douglas-cerrato opened 1 month ago
Here is our understanding of how we will change directions in the matrix to spiral so far.
# If we were to view the matrix in a way where each value
# is the index inside the matrix, it would like something
# like this:
# 00 01 02 00 01 02 03
# 10 11 12 or 10 11 12 13
# 20 21 22 20 21 22 23
# 30 31 32 33
# Therefore, we can use this understanding to figure out how
# we would go about navigating this matrix. This will help us
# in terms of spiraling it.
# To iterate through the matrix by a direction. Here is how
# we would do it.
# Assuming we view each index in this matrix as a (x,y) coordinate
# Right: y + 1
# Down: x + 1
# Left: y - 1
# Up: x - 1
# This would mean we would have to add one position unit to either x or y
# to view the current value at that coordinate. So now just need to figure out
# how to spiral through the matrix (with hopes of doing with the fastest time
# complexity)
This can be found in ee1b8630fd0a59dc7ba6ed47f8c517a608402b56
Spiral Matrix
Given an
m x n
matrix
, return all elements of thematrix
in spiral order.Example 1:
Example 2: