LeetCode 36. Valid Sudoku (Algorithm Explained)

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ม.ค. 2025

ความคิดเห็น • 106

  • @calfland79
    @calfland79 5 ปีที่แล้ว +100

    at 9:08 when he said it is not difficult, my face turned into red. If I've never saw this kind of solution before, I would be doomed in a interview :)

    • @Kitchen-Raccoon4572
      @Kitchen-Raccoon4572 4 ปีที่แล้ว +6

      lol he says that for so many problems and I get kinda sad when I hear that :(

    • @CoolnesXcore
      @CoolnesXcore 4 ปีที่แล้ว +20

      @@Kitchen-Raccoon4572 Don't get sad... Nick is a little arrogant plus he has been doing this for a while. Practice makes perfect!

    • @Turnpost2552
      @Turnpost2552 3 ปีที่แล้ว +1

      What most developers mean that it is relatively easy. Like it is relatively easy to get a phd than to become President. Both are exceptionally hard to get though.

    • @tasneemayham974
      @tasneemayham974 ปีที่แล้ว

      For real!!!!!
      Sheesh I spend 2 hours just to figure out what is happening in my code!!

  • @impos1ble32
    @impos1ble32 5 ปีที่แล้ว +75

    The solution with the single string HashSet looks like pure genius to me.
    Great video!

  • @feavenmundo
    @feavenmundo 6 หลายเดือนก่อน +3

    This honestly was the simplest and best solution i've seen.

  • @ShusenAcademy
    @ShusenAcademy 4 ปีที่แล้ว +11

    Readable code is much better than smart code. Your solution is understandable. Thanks.

  • @Ingenius3
    @Ingenius3 4 ปีที่แล้ว +33

    Huh, the advantages of knowing your data structures and methods...I've spent 10+ hours on a similar sudoku problem and been trying to use only arrays and 2d arrays combined with control statements, and it's been a nightmare to be honest, and now I see this and I really think I gotta learn some data structures, it's ridiculous how fast you can come up a solution like this

    • @webknowledge9989
      @webknowledge9989 2 ปีที่แล้ว +7

      If you feel bad...don't. He spends a lot of time beforehand looking at different solutions, trying to come up with his own solutions, improve it and improving the different solutions he saw. And then, choosing the best one.
      This is pure hardwork.

  • @pradeepanand5296
    @pradeepanand5296 4 ปีที่แล้ว +1

    This is lit. No words. I solved the arcade section of the sudoku by using two hashset one for row and one for column. Never thought this could be the way to optimise the code.

  • @ruchirai5775
    @ruchirai5775 5 ปีที่แล้ว +21

    One word - Genius

    • @rutustup396
      @rutustup396 4 ปีที่แล้ว +2

      The answer were already on leet code. 😅

  • @johnhappy9230
    @johnhappy9230 10 หลายเดือนก่อน

    bro you have the best explanation that i've seen. Thank you so much

  • @shubhamkhurana7545
    @shubhamkhurana7545 5 ปีที่แล้ว +4

    This was a really cool solution Sir, so we can also implement even a string matching algo using string datatype hash set. This problem really teaches a cool concept!! Thank You Sir!!

  • @sicals1720
    @sicals1720 4 ปีที่แล้ว +3

    That moment when he first type the input for strings hash set is just simply mind blowing like "OMG off course it will works"

    • @Last_Resort991
      @Last_Resort991 4 ปีที่แล้ว +2

      It will work better than your grammar teacher.

    • @a.human.
      @a.human. 3 ปีที่แล้ว

      @@Last_Resort991 when the grammar nazi doesn't know grammar himself lol

  • @PrinceKumar-el7ob
    @PrinceKumar-el7ob 3 ปีที่แล้ว

    Exploded my mind what a brilliant way to write aesthetic code man !!

  • @fabusuyiayodeji7016
    @fabusuyiayodeji7016 4 ปีที่แล้ว +2

    Wow! This is the most intuitive solution I've seen to this problem

  • @enthusiasticsimple901
    @enthusiasticsimple901 3 ปีที่แล้ว +1

    Great Solution!!
    Thank Nick for posting these videoss

  • @danieljakel5105
    @danieljakel5105 4 ปีที่แล้ว

    You could also make a list of 81 list with 9 entries each for every field and number that is possible. Then you write a algorithm that cross out each number from the nine possibilieties that has a double in line, colum or quadrat. When all numbers cut one in a list are crossed out, you have found a new one that you can use to delete the other list of posibilities. If each list have just a number its solved!

  • @lugiadark21
    @lugiadark21 4 ปีที่แล้ว +5

    This trick is INSANE!!!! I hope I never get asked a problem like this lol

  • @aba0101
    @aba0101 3 ปีที่แล้ว +1

    Brilliant!! Just to make sure, is the time and space O(1)?

  • @seyikareem8052
    @seyikareem8052 4 ปีที่แล้ว +2

    Curios if it is possible to extend this solution to solve the Sudoku solver problem...

  • @lifeofme3172
    @lifeofme3172 2 ปีที่แล้ว

    WOW amazing solution loved it keep it up dude!

  • @orangetree2104
    @orangetree2104 5 ปีที่แล้ว +1

    Trivia : Nick White still has the hat used in his profile picture, and actually went to Columbia

  • @osamabassam7517
    @osamabassam7517 4 ปีที่แล้ว +2

    A clever solution, but the simple solution (Validating rows, columns, and boxes seperately) is way more faster, for me this solution beat 100% of java solution in term of time, the solution in the video beat 22% only (Which is because of strings hashing I guess).

  • @khanshadab806
    @khanshadab806 4 ปีที่แล้ว +1

    Also, you could remove the big 'if' from the 'for' loop and just check if the size of the set is 3*(Number of non-empty places in the board) after the loop. If it is not, the answer is false.

  • @soumi7356
    @soumi7356 4 ปีที่แล้ว +1

    This was a very different approach !

  • @DeepakAnand-x3f
    @DeepakAnand-x3f ปีที่แล้ว

    Rather than using 3 hashsets using a single hashset was brilliant .

  • @ritamkabiraj8035
    @ritamkabiraj8035 หลายเดือนก่อน

    Wow, what a smart solution bruh !

  • @emanuelcristutiu5854
    @emanuelcristutiu5854 3 ปีที่แล้ว

    Really good explanation, thx mate!

  • @vrindatyagi6744
    @vrindatyagi6744 4 ปีที่แล้ว

    your vids are really helpful THANKS A LOAD

  • @Nickel80
    @Nickel80 4 ปีที่แล้ว +2

    What is the runtime for your solution? Is it O(n^2)?

    • @praharsh_bhatt
      @praharsh_bhatt 4 ปีที่แล้ว

      Yes.

    • @divjyotsinghkhanuja1546
      @divjyotsinghkhanuja1546 4 ปีที่แล้ว +7

      It is O(1). The for loop will always run for 9x9 grid irrespective of input if thats how you came up with O(n).

    • @divjyotsinghkhanuja1546
      @divjyotsinghkhanuja1546 4 ปีที่แล้ว

      @wapanapoli How can the grid size change lol please explain

    • @Mark-tv1zs
      @Mark-tv1zs 2 ปีที่แล้ว

      @@praharsh_bhatt The grid is always 9x9 (even says in the video), so the time complexity is O(1)

  • @iamloking
    @iamloking 2 ปีที่แล้ว

    Loved the solution!

  • @velmuruganr9321
    @velmuruganr9321 3 ปีที่แล้ว

    wow..that blew my mind...I mean wow...
    Im speechless

  • @tushar_agarwal_it_2nd_a_477
    @tushar_agarwal_it_2nd_a_477 3 ปีที่แล้ว +1

    Can anyone give the cpp solution of this question,I am having some issues in my code.Thanks in advance:)

  • @gopalchavan306
    @gopalchavan306 3 ปีที่แล้ว

    wow Awesome, and I was spending hours to just solve this

  • @Jdudeo
    @Jdudeo 4 ปีที่แล้ว +3

    The string concatenation allows using only one map, but what is the point of this? It's nifty, but I don't see a real reason to use this from a practical standpoint

  • @stutipandey2564
    @stutipandey2564 4 ปีที่แล้ว

    This is an awesomeeeeeee solution. I am mesmerized!!!! :)

  • @inderjitsaini9303
    @inderjitsaini9303 3 ปีที่แล้ว

    I tried doing the nested if with normal (seen.add) instead of (!seen.add), return true and return false in outside the for loops. but that doesn't work. Can you tell why?

    • @vishnudarrshanorp3730
      @vishnudarrshanorp3730 ปีที่แล้ว

      in nested if statement you forgot to put ! symbol to check it..

  • @suryakumar-ow8bx
    @suryakumar-ow8bx 2 ปีที่แล้ว

    its showing unexpected return value for return false line... wat to do

  • @NikhilTripathy
    @NikhilTripathy 4 ปีที่แล้ว +1

    WoW! That's so freaking cool..

  • @AbhishekAnand92
    @AbhishekAnand92 2 ปีที่แล้ว

    Brilliant solutions :D

  • @jz-nz6tq
    @jz-nz6tq 4 ปีที่แล้ว

    this method is fucking genius

  • @dangbey4822
    @dangbey4822 2 ปีที่แล้ว

    genious, thank you very much

  • @jordananders8525
    @jordananders8525 3 ปีที่แล้ว +3

    Wouldn't this fail if one of the 3x3 squares looks like
    1 2 3
    4 5 6
    7 8 9
    and if there is another 3x3 square to the left or the right of the first 3x3 square that looks like
    7 4 5
    8 2 3
    6 1 x
    where the x denotes an empty square, then this is an unsolvable Sudoku puzzle.

    • @decisive5760
      @decisive5760 2 ปีที่แล้ว +5

      It's doesn't matter if the sudoku puzzle is unsolvable. This leetcode problem is only checking for the three rules in the description

  • @karana2260
    @karana2260 4 ปีที่แล้ว

    does same number of elements in one hash Set or 3 hash set make a difference on memory usage? after all same units of memory is being allocated but pointers to each memory changes right? the type is same (char)

  • @driveb6591
    @driveb6591 4 ปีที่แล้ว

    Thanks for explaining. What's the time complexity?

  • @Nathan00at78Uuiu
    @Nathan00at78Uuiu 2 ปีที่แล้ว

    I'm not understanding the logic and code for specifying a cube. It makes no sense to me.

    • @decisive5760
      @decisive5760 2 ปีที่แล้ว +1

      It's a 9x9 grid. A box is 3x3. If you divide the index of a row and column by 3, you get the box you're currently in.

  • @CodeSuccessChronicle
    @CodeSuccessChronicle 3 ปีที่แล้ว

    Me: (video put on mute until he claps)

  • @rashedulislam9301
    @rashedulislam9301 5 ปีที่แล้ว

    nice explanation

  • @freesoul2677
    @freesoul2677 3 ปีที่แล้ว +1

    It was a nice AHAAAAAAA moment =D Thank you!

  • @hopechan697
    @hopechan697 4 ปีที่แล้ว

    really helpful thanks :3

  • @kulaisaikumarkulai7319
    @kulaisaikumarkulai7319 3 ปีที่แล้ว

    Great job

  • @SmokyBigSmoke
    @SmokyBigSmoke 4 ปีที่แล้ว

    Thank you so much

  • @ashwinishanbhogue2917
    @ashwinishanbhogue2917 4 ปีที่แล้ว +6

    F R E A K I N G GeniusSSSSSSSSSSS!! :D Mind=blown :)

  • @MommyOfACutie
    @MommyOfACutie 3 ปีที่แล้ว

    This is beautiful

  • @anuraggorkar2299
    @anuraggorkar2299 3 ปีที่แล้ว

    StefanPochmann sends regards ;)

  • @yatinarora1252
    @yatinarora1252 3 ปีที่แล้ว

    LOL if we used three iterations instead of 1 iteration it will return 95% more faster solutions,in case of 1 iteration in c++ 35% only

  • @DavisTibbz
    @DavisTibbz 2 ปีที่แล้ว

    Really? Snake_case in java? 😅 Anyway, cool content bro

  • @WowPlusWow
    @WowPlusWow 4 ปีที่แล้ว +4

    I did this in c++ and got only 6% faster. You also only got 22% This solution is a joke.

    • @_sudipidus_
      @_sudipidus_ 4 ปีที่แล้ว

      Leetcode's system of calculating this is flawed.. I have seen same thing submission showing up as faster than (12%-80%) and then back to 12% within a minute.. (network connection or something)

    • @sudhanshushakya4579
      @sudhanshushakya4579 4 ปีที่แล้ว

      Hye, can you show me your cpp implementation? I am having trouble doing it.
      I used unordered_set in place of HashSet.

    • @bultvidxxxix9973
      @bultvidxxxix9973 4 ปีที่แล้ว +1

      Because this solution is really bad for performance. It's a nice quick solution in a coding interview, but if you want fast execution times, you shouldn't even think about generating strings to hash them...
      It should more look like this:
      public boolean isValidSudoku(char[][] board) {
      for(int i=0; i

    • @yatinarora1252
      @yatinarora1252 3 ปีที่แล้ว

      @@sudhanshushakya4579 ya that will be work fine for rows use ur unordered_sename[i].(arr[i][j]and column use ur unordered set name[j].find(arr[i][j]) and array element arr[i][j] and for boads use following as it is 9^9 matrix use arr[i]j].find(arr[i][j]*3(i/3)*(j/3))

  • @channelname8300
    @channelname8300 4 ปีที่แล้ว

    WTF mannnnnnn...........................Destroyed it like a proooooo

  • @bobered4911
    @bobered4911 ปีที่แล้ว

    Nick no filter white

  • @roshibansal2095
    @roshibansal2095 3 ปีที่แล้ว

    You look like sheldon

  • @algorithmimplementer415
    @algorithmimplementer415 4 ปีที่แล้ว

    looks like you hate to use camelCase in the camelCase world of Java. :D

    • @divjyotsinghkhanuja1546
      @divjyotsinghkhanuja1546 4 ปีที่แล้ว

      He codes in JS except for Data Structures I think thats where the underscores come from ;)

  • @nknidhi321
    @nknidhi321 3 ปีที่แล้ว

    Mind boggling🔥

  • @swatiswagatika2017
    @swatiswagatika2017 3 ปีที่แล้ว

    brilliant !!!!

  • @SudeepDasguptaiginition
    @SudeepDasguptaiginition 4 ปีที่แล้ว

    excellent !!!!

  • @ServantOfLordKrishna.
    @ServantOfLordKrishna. 6 หลายเดือนก่อน +1

    this makes me look dumb

  • @pratheek5096
    @pratheek5096 4 ปีที่แล้ว

    Clever as fuck

  • @MrSharmagaurav03
    @MrSharmagaurav03 2 ปีที่แล้ว

    This algorithm is wrong as it only check for first subset not all 9 subsets.

  • @arjunsuresh27
    @arjunsuresh27 3 ปีที่แล้ว +3

    worst explanation ever!! no intuition or walk through whats so ever cannot explain this in an interview!

  • @charanpasupula3763
    @charanpasupula3763 3 ปีที่แล้ว

    Genius

  • @jenblack8065
    @jenblack8065 4 ปีที่แล้ว

    Explanation according to discussion, no details. Not worth to go through.

  • @julietgeorge4858
    @julietgeorge4858 4 ปีที่แล้ว

    I hope you don't get tired of my comments but this is ridiculous(in a good way )

  • @ketansharma6955
    @ketansharma6955 ปีที่แล้ว

    nice.

  • @demidrek-heyward
    @demidrek-heyward 4 ปีที่แล้ว

    get it

  • @amansaxena4446
    @amansaxena4446 2 ปีที่แล้ว

    we dont know what u mean ,

  • @YOUNGBLOOD-Alex
    @YOUNGBLOOD-Alex 4 ปีที่แล้ว

    WTF

  • @vk1618
    @vk1618 4 ปีที่แล้ว

    Review

  • @ankurkoul3731
    @ankurkoul3731 3 ปีที่แล้ว

    wow :)

  • @ahmedfattah5879
    @ahmedfattah5879 5 ปีที่แล้ว

    18

  • @Alison-yg6qs
    @Alison-yg6qs 5 ปีที่แล้ว

    :-) !!! awesome!!!!!

  • @kedikebba
    @kedikebba 4 ปีที่แล้ว

    Champ!

  • @AmanRaj-tj7lj
    @AmanRaj-tj7lj 3 ปีที่แล้ว

    This does not work. Waste of time