-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_right_intervals.cpp
More file actions
36 lines (31 loc) · 964 Bytes
/
Copy pathfind_right_intervals.cpp
File metadata and controls
36 lines (31 loc) · 964 Bytes
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
// Problem Link : https://leetcode.com/problems/find-right-interval/description/
class Solution {
public:
vector<int> findRightInterval(vector<vector<int>>& intervals) {
int n = intervals.size() ;
vector<int>ans(n , -1);
vector<pair<int , int>>start ;
for(int i = 0 ; i<n ; i++){
start.push_back({intervals[i][0] ,i}) ;
}
sort(start.begin() , start.end());
for(int i =0 ; i<n ; i++){
int end=intervals[i][1] ;
int low= 0 ;
int high = n-1 ;
int res =-1 ;
while(low<=high){
int mid = low+(high-low)/2 ;
if(start[mid].first>=end){
res = start[mid].second ;
high = mid-1 ;
}
else {
low = mid+1 ;
}
}
ans[i]=res ;
}
return ans ;
}
};