forked from super30admin/Binary-Search-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem1.py
57 lines (47 loc) · 1.76 KB
/
problem1.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
""" The problem uses two separate binary seaches to locate the first and the last occurance of the
element. The first binary search finds the target to the left most of the array, and the second
binary search finds the target to the right most of the array.
Time Complexcity: O(log n) + O(log n) = 2*O(log n) = O(log n) since 2 is a constant
Space Complexcity: O(1)
"""
class Solution(object):
def searchRange(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
def search_first_occurance(target, nums ,n):
low = 0
high = n-1
while(low <= high):
mid = low + (high - low)//2
if(nums[mid] == target):
if(mid == 0 or nums[mid] > nums[mid - 1]):
return mid
else:
high = mid - 1
elif (nums[mid] > target):
high = mid - 1
else:
low = mid + 1
return -1
def search_last_occurance(target, nums ,n):
low = 0
high = n-1
while(low <= high):
mid = low + (high - low)//2
if(nums[mid] == target):
if(mid == n-1 or nums[mid] < nums[mid + 1]):
return mid
else:
low = mid + 1
elif (nums[mid] < target):
low = mid + 1
else:
high = mid - 1
return -1
n = len(nums)
first = search_first_occurance(target, nums ,n)
last = search_last_occurance(target, nums ,n)
return [first,last]