Find the Duplicate Number - Floyd's Cycle Detection - Leetcode 287 - Python
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
🚀 neetcode.io/ - I created a FREE site to make interview prep a lot easier, hope it helps! ❤
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
@@nithishthomas1514 ha ha ha ha
@@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]
@@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.
@@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
This is the best video of explanation of Floyd’s algorithm I have seen so far…
This guy is seriously awesome 😎
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!
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.
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!!
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.
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
This is the only explanation of Floyd's algorithm that I could understand... Thanks for this video...
This code s not working
@@IshithaNhe wrote fast[nums[fast]] just change it into nums[nums[fast]] then the code will work fine
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.
I agree with you. The head node is confusing
I was losing my mind tryna understand why the headnode was 0. LOL thanks
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.
@@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 :)
Yes, I had been staring at the screen for quite some time and was searching for this.
That proof explanation was very good.. Even other big channels have failed to do it so smooth. thanks
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
@@apyyymnmn3442 I guess he demoed it at 7:27 but maybe you're looking for mathematical proof?
@@apyyymnmn3442 He did prove it in linked list cycle video
GOAT explanation. Everyone advised me to just memorize the algorithm, you're the only person to explain it to me.
Kudos! No one explained the algorithm better than this, thank you so much
Your explanation was very clear, easily understandable. Thank you
The absolute best explaination I found on CZcams for Floyd's algo
Such an excellent explanation of Floyd's algorithm - thank you NeetCode!
wow bro you are so brutally honest and feels so nice to watch your tutorial
Had to re-watch this to get the mathematical proof, thanks for the great explanation!
great explanation. Especially the maths explanation was really good!!
The proof you made us understand is the best I will ever see for this algo.
Thank you so much, man!
Difficult problem, hard to solve without prior experience on it. Thank you so much.
Amazingly explained ! God bless
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.
Very clean and understandable explanation. I would rewatch it for more times. Thank you for your great work!
Football fans are code monkeys too now. Hazard is finished btw
Up the blues
Fantastic explanation about the math proof of Floyd's algorithm!!!
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
The best and clear explanation I have found on this algorithm so far ! Thank you !!!!!
what a champ!
Thanks for explaining this algo, i had trouble visualising the slow1==slow2 intersecting part.
Your Videos are so useful. ❤️
Thank you for helping me understand Floyd's algorithm.
Never understood this problem until I watched this. Thank you!
Neetly explained! I'm bad with time complexities, I'd love for you to break down the time complexity as well.
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
Amazing explanation as always. Thank you so very much !!!
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❤
Thank you for this very clear explanation!
man, this is such a golden explanation
Amazing explanation as always :)
Thanks so much, to be honest for so many years I could not get this straight until watching this.
You're so good at explaining and you make the subject so fascinating. You're my new Netflix and chill bro.
Great explanation!
sooo good explanation, i watched a bunch of videos, until i landed on your video and got it
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!
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.
@@The6thProgrammer please can you also explain how fast =num[num[fast] is increasing by 2 position at a time.
Honestly, blows my mind how companies expect anyone to derive this relation without having seen it before.
Best explanation so far ❤️
Throwing my Cracking The Coding Interview in the trash... you are all I need
WOW, really loved that proof. Never studied this in uni thanks for your help
Thank you so much for the wonderful explanation! Really liked it
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.
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]]
@@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.
Great Explanation ! Thank you so much !
Great Explanation!
One of the best mathematical explanation
The best explanation of Floyd's algorithm!
very nice and clear explanation really helpful for building logical mind thanks subscribed
whenever i fall in a frenzy over a leetcode problem....i just google it's neetcode solution. Great explanations man.
best explanation so far
I appreciate the brutal honesty at the start of this video lol.
Beautifully explained.
very clear, thank you!
Very good explanation!!!
Thanks for the reassurement that this was a ridiculous question, it totally stumped me haha
Beautiful Explanation
Thank you for this great explanation.
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.
You are really talanted in explaning complicated things, even I got it) Thank you
so good, much appreciate for your effort
you know its a beautiful algorithm when the code for it is so simple
Best explanation for A solution. Respect.
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
Thank you for this content!! The best explanation about this algorithm
thank you for a great explanation!
I will cry if I got this question in intervew
amazing explanation
best intuition explanation
This problem amazed me
best explanation without a doubt.
I got this problem in my interview and the interviewer asked me to explain why this algo works, basically prove it
What company? Google?
beautifully explained
Omg. It was just wow. No one can explain better now. Finally satisfied with Floyd explanation. Thanks a lot sir🙏🙏🙏
you are simply awesome !
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.
I love u dude) Thanks a lot for wonderful explanations)
Wow. Impressive solution.
Thank You for this video
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.
bitwise impl.
JS
function dup(arr){
let num=0
for(let i=0;i
You can also just use sums to calculate the solution - return sum(lst) - len(lst) * (len(lst) - 1) // 2
@@mannymunoz9063 oh, you are correct, I have mistakenly assumed that it would mean that there's only one duplicate and not multiple.
great explanation
Thank you ❤
It's like Leafy teaching me how to land a FAANG job. Respect!
Great explanation! Thanks a lot!
I like how you quickly removed the perimeter around the linked list
Awesome yaar loved it
Thk from Egypt ❤
And to be more accurate, I think it's better to initialize slow and fast with nums[0] instead with 0
Thanks a lot
Amazing video
appreciate you!
sum(nums array) - sum(from 1 to n) ezpz
The intro makes total sense why I wouldnt never think this is a linkedlist problem
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?
I really love u thk for all u do bro