-
Notifications
You must be signed in to change notification settings - Fork 29
/
0034-Find-First-and-Last-Position-of-Element-in-Sorted-Array.py
128 lines (103 loc) · 3.59 KB
/
0034-Find-First-and-Last-Position-of-Element-in-Sorted-Array.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
'''
Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.
If target is not found in the array, return [-1, -1].
NOTE: You must write an algorithm with O(log n) runtime complexity.
Example 1:
Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]
Example 2:
Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]
Example 3:
Input: nums = [], target = 0
Output: [-1,-1]
Constraints:
0 <= nums.length <= 105
-109 <= nums[i] <= 109
nums is a non-decreasing array.
-109 <= target <= 109
'''
from typing import List
# Simple Binary Search
class Solution:
def searchRange(self, nums: List[int], target: int) -> List[int]:
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] == target:
if mid == 0 or nums[mid - 1] != target:
first = mid
break
right = mid - 1
elif nums[mid] > target:
right = mid - 1
else:
left = mid + 1
if left > right:
return [-1, -1]
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] == target:
if mid == len(nums) - 1 or nums[mid + 1] != target:
last = mid
break
left = mid + 1
elif nums[mid] > target:
right = mid - 1
else:
left = mid + 1
return [first, last]
# Simple Binary Search in Two Different Functions
class Solution:
def getFirstOccurence(self, nums, target):
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] == target:
if mid == 0 or nums[mid - 1] != target:
return mid
right = mid - 1
elif nums[mid] > target:
right = mid - 1
else:
left = mid + 1
return -1
def getLastOccurence(self, nums, target):
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] == target:
if mid == len(nums) - 1 or nums[mid + 1] != target:
return mid
left = mid + 1
elif nums[mid] > target:
right = mid - 1
else:
left = mid + 1
return -1
def searchRange(self, nums: List[int], target: int) -> List[int]:
first = self.getFirstOccurence(nums, target)
last = self.getLastOccurence(nums, target)
return [first, last]
# Simple Binary Search with only one Function
class Solution:
def binarySearch(self, nums, target, flag):
low, high = 0, len(nums) - 1
while low <= high:
mid = (low + high) // 2
if nums[mid] > target or (nums[mid] == target and flag):
high = mid - 1
else:
low = mid + 1
return low if flag else high
def searchRange(self, nums: List[int], target: int) -> List[int]:
start = self.binarySearch(nums, target, True)
if start == len(nums) or nums[start] != target:
return [-1, -1]
else:
return [start, self.binarySearch(nums, target, False)]
# Custom Input Check
s = Solution()
answer = s.searchRange([1, 2, 2, 3, 4, 4, 4, 4]) # [4,7]
print(answer)