douglas-cerrato / LeetCode-75

This will just be a folder of the Blind LeetCode 75 problem challenge located here: https://leetcode.com/discuss/general-discussion/460599/blind-75-leetcode-questions
1 stars 0 forks source link

spiralMatrix.py #12

Open douglas-cerrato opened 1 month ago

douglas-cerrato commented 1 month ago

Spiral Matrix


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]
douglas-cerrato commented 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