Valid Sudoku - Leetcode 36 - Hashmaps & Sets (Python)

Поделиться
HTML-код
  • Опубликовано: 9 фев 2025
  • 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...

Комментарии • 26

  • @GregHogg
    @GregHogg  6 месяцев назад

    Master Data Structures & Algorithms For FREE at AlgoMap.io!

  • @karthikistired
    @karthikistired 6 месяцев назад +13

    if someone doesn't wanna use variable starts, you can modify the for loop like this:
    for row in range(0,9,3):
    for col in range(0,9,3):
    s=set()
    for i in range(3):
    for j in range(3):
    if board[row+i][col+j] in s:
    return False
    elif board[row+i][col+j] != '.':
    s.add(board[row+i][col+j])

  • @nguyenkhiem2318
    @nguyenkhiem2318 8 месяцев назад +7

    Thanks Greg, you really have a natural talent for teaching. Was struggling with this one

  • @egg5687
    @egg5687 3 месяца назад +3

    I usually solve the problem and then compare to your solution and for the case of the medium difficulty problems, my solution is often somewhat more convoluted than yours; there's always some detail that I miss that makes my solution significantly more complicated. But for once, I am still pretty happy with my solution even after seeing yours! So I felt compelled to share it:
    class Solution(object):
    def isValidSudoku(self, board):
    """
    :type board: List[List[str]]
    :rtype: bool
    """
    sudoku_dict = {}
    # The format of this dictionary will be number: [{rows},{cols},{box}]
    for i in range(9):
    for j in range(9):
    val = board[i][j]
    if val == ".":
    continue
    box_num = 3 * (i // 3) + (j // 3)
    if val not in sudoku_dict:
    sudoku_dict[val] = [{i}, {j}, {box_num}]
    elif (i in sudoku_dict[val][0]) or (j in sudoku_dict[val][1]) or (box_num in sudoku_dict[val][2]):
    return False
    else:
    sudoku_dict[val][0].add(i)
    sudoku_dict[val][1].add(j)
    sudoku_dict[val][2].add(box_num)
    return True

  • @punnettsquares
    @punnettsquares 5 месяцев назад +8

    Took me 3 years to understand this question

  • @JSH1994
    @JSH1994 4 месяца назад

    your explanations are so clear and intuitive, thank you!

  • @mercytum7045
    @mercytum7045 11 месяцев назад +2

    Thanks, Greg. This was really helpful!

    • @GregHogg
      @GregHogg  11 месяцев назад

      Glad it was helpful!

  • @nathanaelcheramlak9332
    @nathanaelcheramlak9332 11 месяцев назад +2

    Helpful content, Keep going

    • @GregHogg
      @GregHogg  11 месяцев назад

      Thanks a ton 😊

  • @HussainAli-hn2ef
    @HussainAli-hn2ef 9 месяцев назад +1

    very helpful, thanks!

    • @GregHogg
      @GregHogg  9 месяцев назад

      Glad to hear it!!

  • @alek002
    @alek002 4 месяца назад +1

    Thanks!

  • @arsheyajain7055
    @arsheyajain7055 11 месяцев назад

    Super helpful!!!!!

    • @GregHogg
      @GregHogg  11 месяцев назад +1

      Awesome 😎😎

  • @therockriders2759
    @therockriders2759 2 месяца назад

    Super solution

  • @anshpradhan1954
    @anshpradhan1954 5 месяцев назад +3

    "Easily the hardest part" lol

  • @saedf0
    @saedf0 9 месяцев назад +1

    Hey Greg, thanks for the content. Great solution, although i do wonder, how is it BIG O(1) when we have multiple loops? I'm not the best with the complexity identification.

    • @GregHogg
      @GregHogg  9 месяцев назад +4

      Thank you! And that's a great question (at any level). Technically, these loops are going to run a constant amount of times (not dependent on the size of the grid) because WE KNOW the grid will be a 9*9. To scan the grid, it takes exactly 81 positions. Not dependent on some N or something. So O(81) is just O(1) in big o talk. I hope this helps!

    • @saedf0
      @saedf0 9 месяцев назад +1

      @@GregHogg ohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh /*click moment*/
      Man, I cant rely on explanations all the time, do you have any resources/ or easy tricks to remember this stuff.
      I've tried a few videos and identifying it kindve goes over my head
      I get that N^2 (for everytime we do this, run these steps)
      I also understand O(N), Do this
      Log N (Half each time, divide and conquer
      but when the code gets a little longer, kindve difficult to track
      but yh thanks for getting back :)

  • @user-jm6gp2qc8x
    @user-jm6gp2qc8x 5 месяцев назад

    greg anna vera level

  • @krishkchavakrishnakarthik4162
    @krishkchavakrishnakarthik4162 6 месяцев назад +1

    super anna

  • @midhunskani
    @midhunskani 3 месяца назад

    Neat solution

  • @lakku153
    @lakku153 5 месяцев назад

    very nice

  • @kidyoshi007
    @kidyoshi007 5 месяцев назад

    Why are you not "s.clear()" clearing the set after each check for row, col, and box??

    • @michaelleeser-smith9342
      @michaelleeser-smith9342 5 месяцев назад +1

      S is being reinitialized at each step. It's not global so it doesn't need to be cleared