-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
32 lines (25 loc) · 1.01 KB
/
solver.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
class Solution:
def getRange(self, arr, target):
left = self.bin_search(arr, 0, len(arr)-1, target, find_min=True)
right = self.bin_search(arr, 0, len(arr)-1, target, find_min=False)
return [left, right]
def bin_search(self, arr, low, high, target, find_min):
while low <= high:
mid = (high + low) / 2
if arr[mid] == target:
if find_min and self.is_equal_previous(arr, mid):
high = mid - 1
elif not find_min and self.is_equal_next(arr, mid):
low = mid + 1
else:
return mid
else:
if arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
def is_equal_previous(self, arr, index):
return (index-1 >= 0 and arr[index-1] == arr[index])
def is_equal_next(self, arr, index):
return (index+1 < len(arr) and arr[index+1] == arr[index])