Spiral Matrix - Leetcode 54 - Arrays & Strings (Python)

Поділитися
Вставка
  • Опубліковано 15 лис 2024

КОМЕНТАРІ • 15

  • @GregHogg
    @GregHogg  3 місяці тому +2

    Master Data Structures & Algorithms For FREE at AlgoMap.io!

  • @ngneerin
    @ngneerin 5 місяців тому +7

    Simpler solution
    l, r, t, b, res = 0, len(matrix[0]), 0, len(matrix), []
    while l < r and t < b:
    for j in range(l, r):
    res.append(matrix[t][j])
    t += 1
    for i in range(t, b):
    res.append(matrix[i][r - 1])
    r -= 1
    if not (l < r and t < b): break
    for j in range(r - 1, l - 1, -1):
    res.append(matrix[b - 1][j])
    b -= 1
    for i in range(b - 1, t - 1, -1):
    res.append(matrix[i][l])
    l += 1
    return res

  • @dotjs5025
    @dotjs5025 Місяць тому +1

    I am happy I thought about the same solution as you but made some mistakes while executing. Some mistakes I did were ,
    1. Not doing one direction per iteration of the outer while loop. I basically tried to do all the directions one after the other
    2. starting left pointer from 0.
    Thanks for all the amazing work you are doing and helping thousands of engineers

  • @Chibabachacho
    @Chibabachacho Місяць тому

    This is porbably the most intuitive solution I found. Thanks Gregg

  • @chandlerkenworthy3185
    @chandlerkenworthy3185 3 місяці тому +2

    Here is my solution to this problem. It utilises the fact you can just take the outward (perimeter) spiral each time and then select the inner (m-2) x (n-2) elements each time (the inner central matrix) until you run out of elements.
    m, n = len(matrix), len(matrix[0])
    answer = []
    def getPerimeter(mat):
    if len(mat) == 1:
    return mat[0]
    if len(mat[0]) == 1:
    return [i[0] for i in mat]
    return mat[0] + [i[-1] for i in mat[1:-1]] + mat[-1][::-1] + [i[0] for i in mat[1:-1][::-1]]
    while len(answer) != (m * n):
    answer += getPerimeter(matrix)
    matrix = [row[1:-1] for row in matrix[1:-1]]
    return answer

  • @carnagen
    @carnagen 2 місяці тому +1

    Another solution would be to cut off the first row of the matrix and then rotate it to the left. Repeat until only one element remains. It's not faster, however.

  • @barmalini
    @barmalini Місяць тому

    this oneliner is not my solutiion, but works like a charm:
    return matrix and [*matrix.pop(0)] + self.spiralOrder([*zip(*matrix)][::-1])

  • @JoeTan-nq4fq
    @JoeTan-nq4fq Місяць тому +1

    My solution - Add the popped elements (from matrix) to result array over one loop, Recur until matrix is empty
    Step 1 - Pop the first row and add to the result array
    Within the try / catch block
    Step 2 - Pop the elements from rightmost column and add to the result array
    Step 3 - Pop the last row in reverse and add to the result array
    Step 4 - Pop the elements from leftmost column and add to the result array
    Step 5 - Recur until matix is empty - return result + spiralOrder(matrix)
    Catch IndexError (which means matrix is empty) and return result

    • @teddy8038
      @teddy8038 Місяць тому +1

      Implemented this solution and got 0 ms runtime and beat 100%, is this what you got? Also why use recursion, you can just put it in a while loop that runs till the original matrix is empty

    • @JoeTan-nq4fq
      @JoeTan-nq4fq 29 днів тому +1

      @@teddy8038 You can use while loop too. Btw, the runtime result is erratic. I dun get 0 ms runtime n it fluctuates drastically in each run. I reckon using while loop would be faster but I find using recursion is more readable.

    • @Powaup
      @Powaup 21 день тому

      Love the idea of just popping the elements, much easier

    • @Powaup
      @Powaup 21 день тому

      @@teddy8038 I think there's been some sort of bug with Letcode recently as I've gotten 0 ms runtime for a couple answers that I know weren't THAT clever

  • @Powaup
    @Powaup 21 день тому

    came up with a similar idea but couldn't fully code it out. Lord I hope I don't come across this during an interview

  • @floccinau263
    @floccinau263 5 місяців тому

    can you solve this as well?
    981. Time Based Key-Value Store
    Please🙏🏻

  • @vasu_1014
    @vasu_1014 2 місяці тому +1

    Hlo greg what are your thoughts on my code using java
    static List spiralOrder(int[][] m) {
    List list = new ArrayList();
    int size = 0;
    if(m.length %2 == 0)
    size = m.length/2;
    else
    size = m.length/2 + 1;
    for(int i=0; i i)
    j--;
    else
    k--;
    }
    }
    return list;
    }