-
Notifications
You must be signed in to change notification settings - Fork 460
/
Search_For_Range.py
65 lines (58 loc) · 2.26 KB
/
Search_For_Range.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
# Solution #1 - Recursive SOlution
# O(logn) time | O(logn) space
def searchForRange(array, target):
finalRange = [-1, -1]
alteredBinarySearch(array, target, 0, len(array) - 1, finalRange, True)
alteredBinarySearch(array, target, 0, len(array) - 1, finalRange, False)
return finalRange
def alteredBinarySearch(array, target, left, right, finalRange, goLeft):
if left > right:
return
mid = (left + right) // 2
if array[mid] < target:
alteredBinarySearch(array, target, mid + 1, right, finalRange, goLeft)
elif array[mid] > target:
alteredBinarySearch(array, target, left, mid - 1, finalRange, goLeft)
else:
if goLeft:
if mid == 0 or array[mid - 1] != target:
finalRange[0] = mid
else:
alteredBinarySearch(array, target, left, mid - 1, finalRange, goLeft)
else:
if mid == len(array) - 1 or array[mid + 1] != target:
finalRange[1] = mid
else:
alteredBinarySearch(array, target, mid + 1, right, finalRange, goLeft)
# Solution #2 - Iterative SOlution
# O(logn) time | O(1) space
def searchForRange(array, target):
finalRange = [-1, -1]
alteredBinarySearch(array, target, 0, len(array) - 1, finalRange, True)
alteredBinarySearch(array, target, 0, len(array) - 1, finalRange, False)
return finalRange
def alteredBinarySearch(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
else:
right = mid - 1
else:
if mid == len(array) - 1 or array[mid + 1] != target:
finalRange[1] = mid
else:
left = mid + 1
# Solution 3 - using python bisect library
import bisect
def searchForRange(array, target):
# Write your code here.
left = bisect.bisect_left(array, target)
right = bisect.bisect_right(array, target)
return [left, right - 1] if left < right else [-1, -1]