Spiral Matrix - Leetcode 54 - Arrays & Strings (Python)
HTML-код
- Опубликовано: 1 окт 2024
- Master Data Structures & Algorithms for FREE at AlgoMap.io/
Code solutions in Python, Java, C++ and JS for this can be found at my GitHub repo here: github.com/gah...
Complete DSA Pathway Zero to Hero: • Data Structures & Algo...
Please check my playlists for free DSA problem solutions:
• Fundamental DSA Theory
• Array & String Questions
• 2 Pointers Questions
• Sliding Window Questions
• Binary Search Questions
• Stack Questions
• Linked List Questions
• Tree Questions
• Heap Questions
• Recursive Backtracking...
• Graph Questions
• Dynamic Programming (D...
My Data Science & ML RUclips Playlist: • Greg's Path to Become ...
Learn Python and Data Science FASTER at mlnow.ai :)
Support the content: / @greghogg
Follow me on Instagram: / greghogg5
Connect with me on LinkedIn: / greghogg
Follow me on TikTok: / greghogg5
Coursera Plus: imp.i384100.ne...
My Favorite Courses:
Data Structures & Algorithms:
- UCalifornia San Diego DSA: imp.i384100.ne...
- Stanford Algorithms: imp.i384100.ne...
- Python Data Structures: imp.i384100.ne...
- Meta Coding Interview Prep: imp.i384100.ne...
Python:
- UMichigan Python for Everybody: imp.i384100.ne...
- Python Mastery from MLNOW.ai: mlnow.ai/cours...
- Google IT Automation w/ Python: imp.i384100.ne...
Web Dev / Full Stack:
- Meta Front-End Developer: imp.i384100.ne...
- IBM Full Stack Developer: imp.i384100.ne...
- Meta Back-End Developer: imp.i384100.ne...
- John Hopkins HTML, CSS & JS: imp.i384100.ne...
- IBM DevOps: imp.i384100.ne...
Cloud Development:
- AWS Fundamentals: imp.i384100.ne...
- GCP Cloud Engineer: imp.i384100.ne...
- Microsoft Azure Fundamentals: imp.i384100.ne...
Game Development:
- Michigan State Unity Development: imp.i384100.ne...
- UColorado C++ for Unreal Engine: www.coursera.o...
SQL & Data Science:
- SQL by MLNOW.ai: mlnow.ai/cours...
- Python for Data Science by MLNOW.ai: mlnow.ai/cours...
- Google Data Analytics: imp.i384100.ne...
- IBM Data Science: imp.i384100.ne...
- IBM Data Engineer: imp.i384100.ne...
Machine Learning & AI:
- ML Mastery at MLNOW.ai: mlnow.ai/cours...
- ML w/ Andrew Ng: www.coursera.o...
- Deep Learning w/ Andrew Ng: imp.i384100.ne...
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
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.
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
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
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;
}
can you solve this as well?
981. Time Based Key-Value Store
Please🙏🏻