-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsearchForRange.py
86 lines (71 loc) · 2.58 KB
/
searchForRange.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# def searchForRange(array, target):
# history = {}
# for i in range(len(array)-1):
# if array[i] not in history:
# history[array[i]] = [i]
# else:
# history[array[i]].append(i)
# if len(history[target]) == 0:
# return [-1, -1]
# else:
# return [history[target][0], history[target][len(history[target])-1]]
# def searchForRange(array, target):
# finalRange = [-1, -1]
# alteredBinarySearchRange(array, target, 0, len(array)-1, finalRange, True)
# alteredBinarySearchRange(array, target, 0, len(array)-1, finalRange, False)
# return finalRange
# def alteredBinarySearchRange(array, target, left, right, finalRange, goLeft):
# if right < left:
# return
# mid = (left+right)//2
# if array[mid] < target:
# alteredBinarySearchRange(array, target, mid+1,
# right, finalRange, goLeft)
# elif array[mid] > target:
# alteredBinarySearchRange(
# array, target, left, mid-1, finalRange, goLeft)
# else:
# if goLeft:
# if mid == 0 or array[mid-1] != target:
# finalRange[0] = mid
# else:
# alteredBinarySearchRange(
# array, target, left, mid-1, finalRange, goLeft)
# else:
# if mid == len(array)-1 or array[mid+1] != target:
# finalRange[1] = mid
# else:
# alteredBinarySearchRange(
# array, target, mid+1, right, finalRange, goLeft)
# return finalRange
def searchForRange(array, target):
finalRange = [-1, -1]
alteredBinarySearchRange(array, target, 0, len(array)-1, finalRange, True)
alteredBinarySearchRange(array, target, 0, len(array)-1, finalRange, False)
return finalRange
def alteredBinarySearchRange(array, target, left, right, finalRange, goLeft):
while left <= right:
mid = (left+right)//2
if array[mid] < target:
left = mid+1
elif array[mid] > target:
right = mid-1
else:
if goLeft:
if mid == 0 or array[mid-1] != target:
finalRange[0] = mid
return
else:
right = mid-1
else:
if mid == len(array)-1 or array[mid+1] != target:
finalRange[1] = mid
return
else:
left = mid+1
return finalRange
print(
searchForRange(
[0, 1, 21, 33, 45, 45, 45, 45, 45, 45, 61, 71, 73], 45
)
)