-
Notifications
You must be signed in to change notification settings - Fork 1
/
dup.go
94 lines (87 loc) · 1.7 KB
/
dup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package dup
import (
"sort"
)
func FindDuplicate(nums []int) int {
// brute forced
n := len(nums)
if n <= 1 {
return -1
}
// O(n^2)
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
if nums[i] == nums[j] {
return nums[i]
}
}
}
return -1
}
func FindDuplicate2(nums []int) int {
n := len(nums)
if n <= 1 {
return -1
}
// sort nums
sort.Slice(nums, func(i, j int) bool {
return nums[i] <= nums[j]
})
for i := 1; i < n; i++ {
if nums[i] == nums[i-1] {
return nums[i]
}
}
return -1
}
func FindDuplicate3(nums []int) int {
n := len(nums)
if n <= 1 {
return -1
}
/* mathematic proof
* assume at the
* meeting point slow moves s steps, fast moves 2s steps, and the lenght of circle is c
* then we have 2s = s + n * c -> s = n * c -- (1)
* then assume the length from start point to entry point is x
* and the length from entry point to meeting point is a
* there must be s = x + a , so
* x + a = s = n * c
* x = (n-1)*c + (c-a) // c-a means the length from meeting point to entry point
* LHS(x) means the fast tag moves x steps
* RHS means slow moves (n-1) cycles plus the length from the meeting point to entry point
* so we get the entry point
*/
slow, fast := nums[0], nums[nums[0]]
for slow != fast {
slow, fast = nums[slow], nums[nums[fast]]
}
// rest fast to 0
fast = 0
for fast != slow {
fast, slow = nums[fast], nums[slow]
}
return slow
}
func FindDuplicate4(nums []int) int {
n := len(nums)
if n <= 1 {
return -1
}
low, high, mid := 1, n-1, 0
for low < high {
mid = low + (high-low)/2
count := 0
for _, v := range nums {
if v <= mid {
count += 1
}
}
if count > mid {
high = mid
} else {
low = mid + 1
}
}
return low
}