forked from sunstick/code-street
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_for_a_range.cpp
53 lines (39 loc) · 1.21 KB
/
search_for_a_range.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
Given a sorted array of integers, 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].
For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].
*/
class Solution {
public:
// returns the smallest index i such that a[i] >= target
// note that if i < j and a[i] >= target, a[j] >= a[i] >= target
int solve(int a[], int n, int target) {
int lo = 0;
int hi = n - 1;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (a[mid] >= target) hi = mid;
else lo = mid + 1;
}
if (a[lo] != target) return -1;
else return lo;
}
vector<int> searchRange(int A[], int n, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> res;
int x = solve(A, n, target);
res.push_back(x);
if (x == -1) {
res.push_back(-1);
return res;
}
int y = x + 1;
while (y < n && A[y] == target) y++;
res.push_back(y - 1);
return res;
}
};