Spiral Matrix II - Leetcode 59 - Python

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

КОМЕНТАРІ • 16

  • @def__init
    @def__init Рік тому +14

    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

    • @sanjeevmurmu6505
      @sanjeevmurmu6505 Рік тому

      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

    • @rahyousuff
      @rahyousuff Рік тому

      @@sanjeevmurmu6505 right -=1 and bottom -= 1 should be there, "-"(minus) is missing

  • @deadlyecho
    @deadlyecho Рік тому +2

    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

  • @MP-ny3ep
    @MP-ny3ep Рік тому +2

    Thank you for the daily leetcode problems.

  • @Nathan00at78Uuiu
    @Nathan00at78Uuiu Рік тому +4

    Sweeeet. Loving these videos.

  • @Jacky-rc6mu
    @Jacky-rc6mu 21 день тому

    best explain, thanks

  • @rgvhere
    @rgvhere 3 місяці тому

    good solution bro

  • @krateskim4169
    @krateskim4169 Рік тому

    Thank you so much

  • @sanjeevmurmu6505
    @sanjeevmurmu6505 Рік тому

    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

    • @NeetCodeIO
      @NeetCodeIO  Рік тому +2

      It's most likely a typo in your code. Hard to debug without seeing it.

    • @sanjeevmurmu6505
      @sanjeevmurmu6505 Рік тому +1

      ​@@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

    • @cdbrw
      @cdbrw 10 місяців тому

      @@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.

    • @TheAITales
      @TheAITales 2 місяці тому

      @@sanjeevmurmu6505 while left

  • @masternobody1896
    @masternobody1896 Рік тому +2

    Hey man I am poor can you give me advice