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
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
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
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.
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
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
@@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.
@@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
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; }
Master Data Structures & Algorithms For FREE at AlgoMap.io!
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
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
This is porbably the most intuitive solution I found. Thanks Gregg
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
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.
this oneliner is not my solutiion, but works like a charm:
return matrix and [*matrix.pop(0)] + self.spiralOrder([*zip(*matrix)][::-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
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
@@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.
Love the idea of just popping the elements, much easier
@@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
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
can you solve this as well?
981. Time Based Key-Value Store
Please🙏🏻
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;
}