-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindRightInterval.cpp
More file actions
30 lines (30 loc) · 1006 Bytes
/
findRightInterval.cpp
File metadata and controls
30 lines (30 loc) · 1006 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
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class Solution {
public:
struct node{
int value;
int index;
};
vector<int> findRightInterval(vector<Interval>& intervals) {
vector<node> interval(intervals.size()+1);
for (int i = 0; i < intervals.size();i++)interval[i] = {intervals[i].start,i};
auto cmp = [](const node& a, const node& b){return a.value < b.value;};
sort(interval.begin(),interval.end()-1,cmp); // We keep a separate last one in case we cannot find the index
interval.back().index = -1;
vector<int> res(intervals.size());
for (int i = 0; i < intervals.size();i++){
node cur = {intervals[i].end,0};
auto itr = lower_bound(interval.begin(),interval.end()-1,cur,cmp);
res[i] = itr->index;
}
return res;
}
};