-
Notifications
You must be signed in to change notification settings - Fork 0
/
287_find_the_duplicate_number.py
64 lines (60 loc) · 1.65 KB
/
287_find_the_duplicate_number.py
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
def test(n):
out = []
for z in range(1,n):
out.append([int(n*z-(z**2-z)/2), int((n*(n-1)+z*(z+1))/2)])
maxOverlap = 0
maxPair = out[0]
for i in range(len(out)):
pair1 = out[i]
overlaps = 0
for j in range(i+1, len(out)):
pair2 = out[j]
if pair2[0] < pair1[1]:
overlaps += 1
if overlaps > maxOverlap:
maxOverlap = overlaps
maxPair = pair1
print(maxOverlap+1)
print(maxPair)
class Solution(object):
def findDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
left = 1
N = len( nums )
right = N-1
left_count = 0
right_count = 0
mid_count = 0
mid = (left+right)/2
while abs( right-left ) >= 0.25:
mid = (left+right)/2
for num in nums:
if num >= left and num < mid:
left_count += 1
elif num > mid and num <= right:
right_count += 1
elif num == mid:
mid_count += 1
if mid_count > 1:
print("mid > 1")
#return( round( mid ) )
if left_count > right_count:
right = mid
else:
left = mid
left_count = 0
right_count = 0
mid_count = 0
return( round( mid ) )
if __name__ == '__main__':
#nums = [3,1,3,4,2]
#nums = [1,1,2]
#nums = [1,3,4,2,2]
#nums = [1,2,3,4,1]
nums = [8,1,1,9,5,4,2,7,3,6]
#nums = [1,1]
sol = Solution()
print(sol.findDuplicate(nums))