Spiral Matrix II - Leetcode 59 - Python
Vložit
- čas přidán 23. 07. 2024
- 🚀 neetcode.io/ - A better way to prepare for Coding Interviews
Solving Spiral Matrix II - Leetcode 59, today's daily leetcode problem on May 9th.
🥷 Discord: / discord
🐦 Twitter: / neetcode1
🐮 Support the channel: / neetcode
⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
💡 DYNAMIC PROGRAMMING PLAYLIST: • House Robber - Leetco...
Problem Link: leetcode.com/problems/spiral-...
0:00 - Read the problem
1:40 - Drawing Explanation
9:30 - Coding Explanation
leetcode 59
#neetcode #leetcode #python
Two is easier than one because it’s easy to forget to recheck the left / right and top / bottom conditions half way through the main loop due to rectangular cases
does this code work for, I am getting an error "IndexError: list assignment index out of range" Can you explain what is happening
"class Solution:
def generateMatrix(self, n: int) -> List[List[int]]:
mat = [[0] *n for _ in range(n)]
left, right = 0, n - 1
top, bottom = 0, n - 1
val = 1
while left
@@sanjeevmurmu6505 right -=1 and bottom -= 1 should be there, "-"(minus) is missing
We can use a circular linked list of pairs with the directions like this (1,0), (0,1), (-1,0), (0, -1) each time the value we are at satisfies this value - 1% N-1 == 0 we advance our list add the pair to row and col values, value++ and terminate the loop when value == n^2, instead of 4 pointers we have a list of 4 direction pairs, the reminder operation left hand side will decrease by 1 each 3 direction changes
Thank you for the daily leetcode problems.
Sweeeet. Loving these videos.
Thank you so much
does this code work for anybody here, I am getting an error "IndexError: list assignment index out of range" Can anybody explain what is happening
It's most likely a typo in your code. Hard to debug without seeing it.
@@NeetCodeIO
class Solution:
def generateMatrix(self, n: int) -> List[List[int]]:
mat = [[0] *n for _ in range(n)]
left, right = 0, n - 1
top, bottom = 0, n - 1
val = 1
while left
@@sanjeevmurmu6505 after your middle 2 for loops youre doing right = 1 and bottom = 1 instead of decrementing them: right -= 1 and bottom -=1. Just missing the - signs.
Hey man I am poor can you give me advice
dont be poor