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

  Рет қаралды 7,418

Greg Hogg

Greg Hogg

Күн бұрын

Пікірлер: 12
@GregHogg
@GregHogg 3 ай бұрын
Master Data Structures & Algorithms For FREE at AlgoMap.io!
@Chibabachacho
@Chibabachacho 8 күн бұрын
This is porbably the most intuitive solution I found. Thanks Gregg
@dotjs5025
@dotjs5025 7 күн бұрын
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
@ngneerin
@ngneerin 4 ай бұрын
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
@carnagen
@carnagen Ай бұрын
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 15 күн бұрын
this oneliner is not my solutiion, but works like a charm: return matrix and [*matrix.pop(0)] + self.spiralOrder([*zip(*matrix)][::-1])
@chandlerkenworthy3185
@chandlerkenworthy3185 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
@floccinau263
@floccinau263 4 ай бұрын
can you solve this as well? 981. Time Based Key-Value Store Please🙏🏻
@JoeTan-nq4fq
@JoeTan-nq4fq 25 күн бұрын
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 6 күн бұрын
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 5 күн бұрын
@@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.
@vasu_1014
@vasu_1014 2 ай бұрын
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; }
Coin Change - Leetcode 322 - Dynamic Programming (Python)
15:27
Rotate Image - Leetcode 48 - Arrays & Strings (Python)
5:27
Greg Hogg
Рет қаралды 8 М.
Sigma baby, you've conquered soap! 😲😮‍💨 LeoNata family #shorts
00:37
Как не носить с собой вещи
00:31
Miracle
Рет қаралды 1,2 МЛН
Spiral Matrix - Microsoft Interview Question - Leetcode 54
16:46
Merge Sort
12:20
Profound Academy
Рет қаралды 288
Koko Eating Bananas - Leetcode 875 - Binary Search (Python)
13:34
Spiral Traversal of a Matrix | Spiral Matrix
16:33
take U forward
Рет қаралды 226 М.
Sigma baby, you've conquered soap! 😲😮‍💨 LeoNata family #shorts
00:37