-
Notifications
You must be signed in to change notification settings - Fork 26
/
34.FindFirstandLastPositionofElementinSortedArray(BinarySearch).py
50 lines (44 loc) · 1.57 KB
/
34.FindFirstandLastPositionofElementinSortedArray(BinarySearch).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
"""
Given an array of integers nums sorted in ascending order, find the starting
and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1].
Example:
Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]
"""
#Difficulty: Medium
#88 / 88 test cases passed.
#Runtime: 84 ms
#Memory Usage: 15.3 MB
#Runtime: 84 ms, faster than 86.58% of Python3 online submissions for Find First and Last Position of Element in Sorted Array.
#Memory Usage: 15.3 MB, less than 7.83% of Python3 online submissions for Find First and Last Position of Element in Sorted Array.
class Solution:
def searchRange(self, nums: List[int], target: int) -> List[int]:
if not nums:
return [-1, -1]
length = len(nums)
l = 0
r = length - 1
while l + 1 < r:
m = (l + r) // 2
if nums[m] == target:
l = m - 1
while l > -1 and nums[l] == target:
l -= 1
r = m + 1
while r < length and nums[r] == target:
r += 1
return [l+1, r-1]
if nums[m] < target:
l = m
else:
r = m
'''Сrutches for short arrays'''
if nums[l] == target and nums[r] == target:
return [l, r]
if nums[l] == target:
return [l, l]
if nums[r] == target:
return [r, r]
return [-1, -1]