Spiral Matrix II - Leetcode 59 - Python

Sdílet
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

Komentáře • 13

  • @def__init
    @def__init Před rokem +11

    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 Před rokem

      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 Před 11 měsíci

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

  • @deadlyecho
    @deadlyecho Před rokem +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 Před rokem +2

    Thank you for the daily leetcode problems.

  • @Nathan00at78Uuiu
    @Nathan00at78Uuiu Před rokem +4

    Sweeeet. Loving these videos.

  • @krateskim4169
    @krateskim4169 Před rokem

    Thank you so much

  • @sanjeevmurmu6505
    @sanjeevmurmu6505 Před rokem

    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  Před rokem +2

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

    • @sanjeevmurmu6505
      @sanjeevmurmu6505 Před rokem +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 Před 6 měsíci

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

  • @masternobody1896
    @masternobody1896 Před rokem +2

    Hey man I am poor can you give me advice