Find the Duplicate Number - Floyd's Cycle Detection - Leetcode 287 - Python

Sdílet
Vložit
  • čas přidán 2. 07. 2024
  • 🚀 neetcode.io/ - A better way to prepare for Coding Interviews
    🐦 Twitter: / neetcode1
    🥷 Discord: / discord
    🐮 Support the channel: / neetcode
    Twitter: / neetcode1
    Discord: / discord
    ⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
    💡 CODING SOLUTIONS: • Coding Interview Solut...
    💡 DYNAMIC PROGRAMMING PLAYLIST: • House Robber - Leetco...
    🌲 TREE PLAYLIST: • Invert Binary Tree - D...
    💡 GRAPH PLAYLIST: • Course Schedule - Grap...
    💡 BACKTRACKING PLAYLIST: • Word Search - Backtrac...
    💡 LINKED LIST PLAYLIST: • Reverse Linked List - ...
    Problem Link: neetcode.io/problems/find-dup...
    0:00 - Read the problem
    2:32 - Drawing Explanation
    14:31 - Coding Explanation
    leetcode 287
    This question was identified as an interview question from here: github.com/xizhengszhang/Leet...
    #floyd #cycle #python
    Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
  • Věda a technologie

Komentáře • 473

  • @NeetCode
    @NeetCode  Před 3 lety +38

    🚀 neetcode.io/ - I created a FREE site to make interview prep a lot easier, hope it helps! ❤

    • @nithishthomas1514
      @nithishthomas1514 Před rokem +3

      Can't you just use sum of arithmetic progression to find the ans quicker?
      1,2,3,4,2
      If there were n numbers in range 1 to n their sum would be n×(n+1)/2 = 10.
      Actual sum = 12
      12- 10 = 2
      1,2,3,3 =9
      n*(n+1)/2 = 6
      9-6 = 3

    • @hhcdghjjgsdrt235
      @hhcdghjjgsdrt235 Před rokem

      @@nithishthomas1514 ha ha ha ha

    • @onkarsingh-vu1ds
      @onkarsingh-vu1ds Před rokem +3

      @@nithishthomas1514 was my 1st intuition too. I even submitted the code with this solution.
      Maybe the language in the question is ambiguous.
      The input can be [2,2,2,2,2] or [1,2,3,2,2]

    • @leventoz9530
      @leventoz9530 Před 10 měsíci

      @@onkarsingh-vu1ds It says there are n+1 numbers from 1-n and only one repeated number, so all numbers from 1 - n must be present, plus the extra. @nithishthomas1514 appears correct.

    • @leungcheng4086
      @leungcheng4086 Před 5 měsíci

      @@leventoz9530 This problem now actually have a testcase that the input is [2,2,2,2,2] although this is contradict to the problem description. I know that because I tried to submit with this solution lol

  • @JihChiLee
    @JihChiLee Před 3 lety +295

    This is the best video of explanation of Floyd’s algorithm I have seen so far…

  • @shelllu6888
    @shelllu6888 Před 2 lety +188

    Your 1min introduction on this problem literally cracked up me :D Never think of saltiness can turn into such a fun intro. Will probably remember this problem and how to solve it for a long time :D Thanks for making this video!

    • @DanielRodrigues-bx6lr
      @DanielRodrigues-bx6lr Před rokem +6

      I'll also always remember it because JomaTech made a meme video on it, and I tried learning it after that but failed miserably. Then NeetCode explains it flawlessly in one video. xD A great teacher can make all the difference.

  • @leungcheng4086
    @leungcheng4086 Před 5 měsíci +17

    Thank you so much for the video of clear explaination!
    For the graph starting from 2:32, I think the outgoing pointer from node 0 should point at node 1 first. And then node 1 will point to node 3.
    Skipping the node that is pointed by 0 will struggle at the situation that element in 0 index is already the anwser.
    At last, really thank you for creating great website needcode with good videos of the explaination of solution!!

  • @rajeshseptember09
    @rajeshseptember09 Před 8 měsíci +24

    That's right. Even Floyd wouldn't have resolved this problem in 30 minutes. It goes on to say that given the nature of such requirements in an interview, certain solutions are best memorized so that you could use them in a different situation or if the same problem comes up again.

  • @epsilonator
    @epsilonator Před 2 lety +56

    I absolutely love the fact that you code the solution right in front of us while explaining it, whereas other coding CZcamsrs just show their already written code which sometimes becomes difficult to understand. Thanks a ton for your awesome videos

  • @interviewprep6827
    @interviewprep6827 Před 3 lety +95

    This is the only explanation of Floyd's algorithm that I could understand... Thanks for this video...

    • @IshithaN
      @IshithaN Před rokem +1

      This code s not working

    • @MohitKumar-dy3ep
      @MohitKumar-dy3ep Před 3 měsíci

      ​@@IshithaNhe wrote fast[nums[fast]] just change it into nums[nums[fast]] then the code will work fine

  • @123climbmytree
    @123climbmytree Před rokem +30

    I believe the head node at 2:35 should be 1 and not 0.
    1 --> 3 --> 2 4.
    This may cause confusion for others, thought I'd point it out.

    • @guosun8090
      @guosun8090 Před rokem +4

      I agree with you. The head node is confusing

    • @osamaahmad8113
      @osamaahmad8113 Před 7 měsíci +3

      I was losing my mind tryna understand why the headnode was 0. LOL thanks

    • @shellyyang1916
      @shellyyang1916 Před 7 měsíci +14

      Actually, it should be 0 --> 1 --> 3 --> 2 4, so Neetcode forgot to draw the node "0", which is never reached. Because notice now slow and fast are starting from 0, rather than nums[0]. Hence, in the first step, slow will become 1, and fast will become 3. If they both starts from "1", then at the first step, slow will goes to 3, and fast will goes to 2, which would not work.

    • @shdnas6695
      @shdnas6695 Před 6 měsíci

      @@shellyyang1916 why 0 1 3 2 4. Why this sequence? i mean the initial give was 1 3 4 2 2, so how it became 0 --> 1 --> 3 --> 2 4 :)

    • @vroomerlifts
      @vroomerlifts Před měsícem +1

      Yes, I had been staring at the screen for quite some time and was searching for this.

  • @joeycopperson
    @joeycopperson Před 2 lety +46

    That proof explanation was very good.. Even other big channels have failed to do it so smooth. thanks

    • @apyyymnmn3442
      @apyyymnmn3442 Před 6 měsíci +1

      The proof isn't 100% right as he takes for granted that fast and slow will intersect though. But apart from that, everything was great

    • @joeycopperson
      @joeycopperson Před 6 měsíci

      @@apyyymnmn3442 I guess he demoed it at 7:27 but maybe you're looking for mathematical proof?

    • @birukabza
      @birukabza Před měsícem

      @@apyyymnmn3442 He did prove it in linked list cycle video

  • @akankshasharma7498
    @akankshasharma7498 Před rokem +13

    GOAT explanation. Everyone advised me to just memorize the algorithm, you're the only person to explain it to me.

  • @unmeshchougule5666
    @unmeshchougule5666 Před rokem +10

    Kudos! No one explained the algorithm better than this, thank you so much

  • @anshaneja804
    @anshaneja804 Před 3 lety +11

    Your explanation was very clear, easily understandable. Thank you

  • @vikaskumarl3719
    @vikaskumarl3719 Před 2 lety +1

    The absolute best explaination I found on CZcams for Floyd's algo

  • @user-lj4et5jk9q
    @user-lj4et5jk9q Před rokem +2

    Such an excellent explanation of Floyd's algorithm - thank you NeetCode!

  • @Abc-lr4bf
    @Abc-lr4bf Před rokem

    wow bro you are so brutally honest and feels so nice to watch your tutorial

  • @clintondannolfo714
    @clintondannolfo714 Před 2 lety +6

    Had to re-watch this to get the mathematical proof, thanks for the great explanation!

  • @kishoremohanavelu2968
    @kishoremohanavelu2968 Před 2 lety +3

    great explanation. Especially the maths explanation was really good!!

  • @rahulsbhatt
    @rahulsbhatt Před rokem

    The proof you made us understand is the best I will ever see for this algo.
    Thank you so much, man!

  • @tanoybhowmick8715
    @tanoybhowmick8715 Před 2 lety +9

    Difficult problem, hard to solve without prior experience on it. Thank you so much.

  • @niharbastia5740
    @niharbastia5740 Před 2 lety +3

    Amazingly explained ! God bless

  • @aadityakiran_s
    @aadityakiran_s Před rokem

    Very well explained. Thank you. It's pretty simple if you've already seen it. You're right. I also learnt a lot form the other solutions posted on LeetCode.

  • @harpercfc_
    @harpercfc_ Před 2 lety +6

    Very clean and understandable explanation. I would rewatch it for more times. Thank you for your great work!

    • @johnpaul4301
      @johnpaul4301 Před rokem +6

      Football fans are code monkeys too now. Hazard is finished btw

    • @yskida5
      @yskida5 Před rokem

      Up the blues

  • @linli7049
    @linli7049 Před 3 lety +1

    Fantastic explanation about the math proof of Floyd's algorithm!!!

  • @rekhasrivastava5114
    @rekhasrivastava5114 Před 2 lety

    Best Explanation
    I was having idea of Floyd algorithm but I was not clear about it.
    Your explanation clears my all doubt
    Thank You so much

  • @vdyb745
    @vdyb745 Před 2 lety +3

    The best and clear explanation I have found on this algorithm so far ! Thank you !!!!!

  • @deresel4874
    @deresel4874 Před rokem

    what a champ!
    Thanks for explaining this algo, i had trouble visualising the slow1==slow2 intersecting part.

  • @TechnicalStudio
    @TechnicalStudio Před 2 lety +2

    Your Videos are so useful. ❤️

  • @riteshsaha6881
    @riteshsaha6881 Před 2 lety

    Thank you for helping me understand Floyd's algorithm.

  • @AnshViswanathan
    @AnshViswanathan Před 21 dnem

    Never understood this problem until I watched this. Thank you!

  • @vedantshinde2277
    @vedantshinde2277 Před 2 lety

    Neetly explained! I'm bad with time complexities, I'd love for you to break down the time complexity as well.

  • @RM-bg5cd
    @RM-bg5cd Před rokem +4

    These are the kind of questions that piss me the fuck off. if you get this question in an interview and missed out on class it's GG
    Thank you for your explanation as always though, this channel is a gold standard

  • @MP-ny3ep
    @MP-ny3ep Před 9 měsíci +1

    Amazing explanation as always. Thank you so very much !!!

  • @jkyu2701
    @jkyu2701 Před 5 měsíci +1

    I don’t like this type of interview problems either but have no choice but to solve it to get prepared. Big Thanks for your great video, it helped a lot❤

  • @wouk1805
    @wouk1805 Před rokem

    Thank you for this very clear explanation!

  •  Před 2 lety +1

    man, this is such a golden explanation

  • @sezzario
    @sezzario Před 2 lety +2

    Amazing explanation as always :)

  • @ruxiz2007
    @ruxiz2007 Před 2 lety

    Thanks so much, to be honest for so many years I could not get this straight until watching this.

  • @user-kf5uz6rw4w
    @user-kf5uz6rw4w Před 3 měsíci

    You're so good at explaining and you make the subject so fascinating. You're my new Netflix and chill bro.

  • @singletmat5172
    @singletmat5172 Před 3 lety +1

    Great explanation!

  • @tkr_kun
    @tkr_kun Před 2 lety

    sooo good explanation, i watched a bunch of videos, until i landed on your video and got it

  • @bharathithal8299
    @bharathithal8299 Před 2 lety +20

    Great solution and explanation but there's a small mistake in the linked list diagram. The first element is actually 1 and not 0.
    Great work, thanks a lot!

    • @The6thProgrammer
      @The6thProgrammer Před 9 měsíci +1

      Yes and no. While you are correct that 1 should appear in the linked list visual, I think it should be (0)->(1) at the beginning because the 0 node is technically our head (always) that no other node will ever point to. When we are setting slow and fast to 0 we are basically starting at the head. This makes it clearer to think about conceptually since we can consider fast and slow as starting together on node 0. Otherwise it is unclear in the linked list visual where fast and slow begin.

    • @surajthapa5808
      @surajthapa5808 Před 4 měsíci

      @@The6thProgrammer please can you also explain how fast =num[num[fast] is increasing by 2 position at a time.

  • @tedcat117
    @tedcat117 Před 2 měsíci +2

    Honestly, blows my mind how companies expect anyone to derive this relation without having seen it before.

  • @systemforge
    @systemforge Před 2 lety

    Best explanation so far ❤️

  • @teamo8033
    @teamo8033 Před rokem

    Throwing my Cracking The Coding Interview in the trash... you are all I need

  • @prammar1951
    @prammar1951 Před 8 měsíci

    WOW, really loved that proof. Never studied this in uni thanks for your help

  • @Cheng-K
    @Cheng-K Před 9 měsíci

    Thank you so much for the wonderful explanation! Really liked it

  • @vijayschadalavada
    @vijayschadalavada Před rokem +1

    This is one hell of a tricky problem. I did it with hashset and thought I'm done but I'm not.
    Appreciate your explanation. Very intuitive. I had re-watch to understand better.

    • @astik2002
      @astik2002 Před rokem

      hey can you explain me how fast = nums[nums[fast]] is moving two times? its clearly just one step ahead of slow pointer which is slow = nums[slow]]

    • @user-ct6vn8ux2g
      @user-ct6vn8ux2g Před 11 měsíci

      @@astik2002
      the default value of slow, fast is 0, which is a default pointer point to the first element in the nums array.
      each value in the nums array is a pointer to next position (value 3 means point to index 3, value 2 means point to index 2), not the adjacent position. the next position may be adjacent position, may not.
      so nums[fast] moves fast pointer to the next position, nums[nums[fast]] move fast pointer to the next position again.
      compared with slow = nums[slow], which just move slow pointer to the next position one time.

  • @hangduong6683
    @hangduong6683 Před 6 měsíci

    Great Explanation ! Thank you so much !

  • @vishaldamgude2737
    @vishaldamgude2737 Před 2 lety

    Great Explanation!

  • @gauravgupta5530
    @gauravgupta5530 Před 2 lety

    One of the best mathematical explanation

  • @jaythebadyo
    @jaythebadyo Před 2 lety

    The best explanation of Floyd's algorithm!

  • @mixshots1801
    @mixshots1801 Před rokem

    very nice and clear explanation really helpful for building logical mind thanks subscribed

  • @chetansingh2901
    @chetansingh2901 Před rokem

    whenever i fall in a frenzy over a leetcode problem....i just google it's neetcode solution. Great explanations man.

  • @vedantchourasia2842
    @vedantchourasia2842 Před 2 lety

    best explanation so far

  • @rajastylez
    @rajastylez Před 2 lety

    I appreciate the brutal honesty at the start of this video lol.

  • @abhijl
    @abhijl Před 2 měsíci

    Beautifully explained.

  • @Aalii6
    @Aalii6 Před 2 lety

    very clear, thank you!

  • @sannge6471
    @sannge6471 Před rokem

    Very good explanation!!!

  • @dk20can86
    @dk20can86 Před 2 lety

    Thanks for the reassurement that this was a ridiculous question, it totally stumped me haha

  • @jagdeepsingh5699
    @jagdeepsingh5699 Před 9 měsíci

    Beautiful Explanation

  • @hasbealam
    @hasbealam Před rokem

    Thank you for this great explanation.

  • @finemechanic
    @finemechanic Před 3 měsíci

    Great explanation of everything starting with why it is a linked list and why it has to have a cycle and ending with the Floyd's algorithm.

  • @user-vn1yf4bi1y
    @user-vn1yf4bi1y Před 11 měsíci

    You are really talanted in explaning complicated things, even I got it) Thank you

  • @sase1017
    @sase1017 Před 11 měsíci

    so good, much appreciate for your effort

  • @faiqito6987
    @faiqito6987 Před 9 měsíci +1

    you know its a beautiful algorithm when the code for it is so simple

  • @damandeepdhillon4592
    @damandeepdhillon4592 Před měsícem

    Best explanation for A solution. Respect.

  • @user-ge4lv2kz6d
    @user-ge4lv2kz6d Před 2 měsíci +1

    You know what when you said that it is very difficult to solve this in 30 mins even for the person who invented the algorithm made me relief. Thanks

  • @matheusmenezes4702
    @matheusmenezes4702 Před 4 měsíci

    Thank you for this content!! The best explanation about this algorithm

  • @meylyssa3666
    @meylyssa3666 Před 3 měsíci

    thank you for a great explanation!

  • @tinymurky7329
    @tinymurky7329 Před 8 měsíci +2

    I will cry if I got this question in intervew

  • @techup9893
    @techup9893 Před rokem

    amazing explanation

  • @subhampatar3266
    @subhampatar3266 Před 2 lety

    best intuition explanation

  • @the-tankeur1982
    @the-tankeur1982 Před 11 měsíci

    This problem amazed me

  • @pranchalgupta2370
    @pranchalgupta2370 Před 2 měsíci

    best explanation without a doubt.

  • @krishankantray
    @krishankantray Před 4 měsíci +1

    I got this problem in my interview and the interviewer asked me to explain why this algo works, basically prove it

  • @simranbirla6368
    @simranbirla6368 Před 5 měsíci

    beautifully explained

  • @piyushraj8701
    @piyushraj8701 Před rokem +3

    Omg. It was just wow. No one can explain better now. Finally satisfied with Floyd explanation. Thanks a lot sir🙏🙏🙏

  • @TechOnScreen
    @TechOnScreen Před 2 lety

    you are simply awesome !

  • @bigbigdog
    @bigbigdog Před 6 měsíci +1

    I was asked this question during a tech screen. Was not able to figure it out on my own and I ended up doing the sorting solution.

  • @algosavage7057
    @algosavage7057 Před 2 lety

    I love u dude) Thanks a lot for wonderful explanations)

  • @andriidanylov9453
    @andriidanylov9453 Před rokem

    Wow. Impressive solution.

  • @Sumeet_100
    @Sumeet_100 Před 9 měsíci

    Thank You for this video

  • @AnonYmous-yu6hv
    @AnonYmous-yu6hv Před 9 měsíci +1

    You're right, I don't know why would anyone ask this question, nobody will know it unless they have super memory and saw it already and this doesn't let you learn anything about the interviewee.

  • @shanemarchan658
    @shanemarchan658 Před rokem

    bitwise impl.
    JS
    function dup(arr){
    let num=0
    for(let i=0;i

  • @abyxatjov4018
    @abyxatjov4018 Před rokem +2

    You can also just use sums to calculate the solution - return sum(lst) - len(lst) * (len(lst) - 1) // 2

    • @abyxatjov4018
      @abyxatjov4018 Před rokem

      @@mannymunoz9063 oh, you are correct, I have mistakenly assumed that it would mean that there's only one duplicate and not multiple.

  • @sebastianacostamolina9593
    @sebastianacostamolina9593 Před 10 měsíci

    great explanation

  • @jegadheeswarank6290
    @jegadheeswarank6290 Před 2 měsíci

    Thank you ❤

  • @nevermore7755
    @nevermore7755 Před 2 lety

    It's like Leafy teaching me how to land a FAANG job. Respect!

  • @kopalniranjan7513
    @kopalniranjan7513 Před 3 lety +1

    Great explanation! Thanks a lot!

  • @supercarpro
    @supercarpro Před rokem

    I like how you quickly removed the perimeter around the linked list

  • @user-ge4lv2kz6d
    @user-ge4lv2kz6d Před 2 měsíci

    Awesome yaar loved it

  • @anassalah838
    @anassalah838 Před 9 měsíci

    Thk from Egypt ❤

  • @algosavage7057
    @algosavage7057 Před 2 lety +1

    And to be more accurate, I think it's better to initialize slow and fast with nums[0] instead with 0

  • @amrholo4445
    @amrholo4445 Před 3 měsíci

    Thanks a lot

  • @terryzo
    @terryzo Před rokem

    Amazing video

  • @hoyinli7462
    @hoyinli7462 Před 2 lety

    appreciate you!

  • @BM-zy8qo
    @BM-zy8qo Před rokem +1

    sum(nums array) - sum(from 1 to n) ezpz

  • @fangzhengchen7620
    @fangzhengchen7620 Před rokem

    The intro makes total sense why I wouldnt never think this is a linkedlist problem

  • @nothingisreal6345
    @nothingisreal6345 Před rokem +1

    I mean for loop detection it is useful. But could it be used for the general case to detect if an array of objects contains some duplicates?

  • @anassalah838
    @anassalah838 Před 9 měsíci

    I really love u thk for all u do bro