Repository navigation
Expand file tree
/
Copy pathsearchRange.py
More file actions
59 lines (51 loc) · 1.51 KB
/
Copy pathsearchRange.py
File metadata and controls
59 lines (51 loc) · 1.51 KB
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
# First and Last Position of X in Sorted Array
# Facebook Coding Interview Question
def find_first_pos(vec, target):
n = len(vec)
first_pos = n
low = 0
high = n-1
while (low <= high):
#mid = int(low + (high - low) /2)
mid = low + (high - low) //2
if(vec[mid]>= target):
first_pos = mid
high = mid - 1
else: # vec[mid]< target
low = mid+1
return first_pos
def searchRange(vec, target):
first = find_first_pos(vec, target)
last = find_first_pos(vec, target+1) - 1
if(first <= last):
return [first, last]
return [-1, -1]
# driver code//
print(searchRange( [5,7,7,8,8,10], 8))
print(searchRange( [5,7,7,8,8,10], 6))
print(searchRange( [1, 1], 1))
print(searchRange( [4, 5, 4], 4))
#https://www.youtube.com/watch?v=dVXy6hmE_0U
#############
'''
class Solution:
def find_first_pos(self, vec: List[int], target: int):
n = len(vec)
first_pos = n
low = 0
high = n-1
while (low <= high):
mid = int(low + (high - low) /2)
if(vec[mid]>= target):
first_pos = mid
high = mid - 1
else: # vec[mid]< target
low = mid+1
return first_pos
def searchRange(self, nums: List[int], target: int) -> List[int]:
first = self.find_first_pos(nums, target)
last = self.find_first_pos(nums, target+1) - 1
if(first <= last):
return [first, last]
return [-1, -1]
'''