-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary-Search-436. Find Right Interval.cpp
62 lines (40 loc) · 1.64 KB
/
Binary-Search-436. Find Right Interval.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
54
55
56
57
58
59
60
61
62
class Solution {
public:
/* the idea is to make vector pair of start time and index and then check
is there any start time greater than equal to current interval end time if yes
then push its position into the vector else push -1 into the vector*/
int binarySearch(vector<pair<int,int>>&nums, int key){
int low=0,high=nums.size()-1;
int val=nums.size();
while(low<=high){
int mid=(low+high)/2;
if(nums[mid].first>=key){
high=mid-1;
val=nums[mid].second;
}
else{
low=mid+1;
}
}
return val;
}
vector<int> findRightInterval(vector<vector<int>>& intervals) {
vector<pair<int,int>>nums;
vector<int>res;
int n=intervals.size();
for(int i=0;i<n;i++){
nums.push_back({intervals[i][0],i});
}
sort(nums.begin(),nums.end());
for(int i=0;i<n;i++){
int val=intervals[i][1];
int pos=binarySearch(nums,val);
if(pos==n){
res.push_back(-1);
}else{
res.push_back(pos);
}
}
return res;
}
};