Skip to content

Commit 5f882d9

Browse files
committed
docs: add algorithm description in find the duplicate number
1 parent ccf3913 commit 5f882d9

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/medium/find_the_duplicate_number.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,27 @@ pub fn find_duplicate(nums: Vec<i32>) -> i32 {
2323

2424
ptr1
2525
}
26+
27+
/*
28+
Algorithm - Floyd's Tortoise and Hare (Cycle Detection)
29+
------------------------------------------------------
30+
1. Initialize tortoise and hare to the first element of the array.
31+
2. Move tortoise to nums[tortoise].
32+
3. Move hare to nums[nums[hare]].
33+
4. Repeat steps 2 and 3 until tortoise == hare.
34+
5. Initialize ptr1 to the first element of the array.
35+
6. Initialize ptr2 to tortoise.
36+
7. Move ptr1 to nums[ptr1].
37+
8. Move ptr2 to nums[ptr2].
38+
9. Repeat steps 7 and 8 until ptr1 == ptr2.
39+
10. Return ptr1.
40+
41+
Time Complexity: O(n)
42+
Space Complexity: O(1)
43+
44+
How works Floyd's Tortoise and Hare?
45+
------------------------------------
46+
https://youtu.be/wjYnzkAhcNk?t=145
47+
48+
49+
*/

0 commit comments

Comments
 (0)