Created
August 19, 2020 12:17
-
-
Save iamprayush/ca4d1d00abbb8410a4801b5188d13f05 to your computer and use it in GitHub Desktop.
Find the Duplicate Number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def findDuplicate(self, arr: List[int]) -> int: | |
# Awesome proof here: https://youtu.be/9YTjXqqJEFE?t=431 | |
hare, tortoise = 0, 0 | |
low_speed = False | |
while True: | |
if low_speed: | |
hare = arr[hare] | |
else: | |
hare = arr[arr[hare]] | |
tortoise = arr[tortoise] | |
if hare == tortoise: | |
if low_speed: | |
return hare | |
hare = 0 | |
low_speed = True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment