forked from super30admin/Binary-Search-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem1.cpp
38 lines (38 loc) · 1.23 KB
/
problem1.cpp
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
/*
Time complexity - O(logN)
Space complexity - O(1)
*/
class Solution {
private:
int leftBinSearch(vector<int>& nums, int target){
int l = 0, r = nums.size() - 1;
while (l <= r){
int mid = l + (r - l) / 2;
if (nums[mid] < target) l = mid + 1;
else if (((mid > 0) && (nums[mid] == target && nums[mid - 1] == target))
|| nums[mid] > target) r = mid - 1;
else return mid;
}
return -1;
}
int rightBinSearch(vector<int>& nums, int target){
int l = 0, r = nums.size() - 1;
while (l <= r){
int mid = l + (r - l) / 2;
if (nums[mid] > target) r = mid - 1;
else if (((mid < nums.size() - 1) && (nums[mid] == target && nums[mid + 1] == target))
|| nums[mid] < target) l = mid + 1;
else return mid;
}
return -1;
}
public:
vector<int> searchRange(vector<int>& nums, int target) {
if (nums.size() == 0) return {-1,-1};
//find lower idx
int leftIdx = leftBinSearch(nums, target);
if (leftIdx == -1) return {-1, -1};
int rightIdx = rightBinSearch(nums, target);
return {leftIdx, rightIdx};
}
};