-
Notifications
You must be signed in to change notification settings - Fork 77
/
solution.cpp
41 lines (39 loc) · 993 Bytes
/
solution.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
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
bool cmp(pair<Interval,int> a,pair<Interval,int> b)
{
return a.first.start <= b.first.start || (a.first.start==b.first.start&&a.first.end<=b.first.end);
}
class Solution
{
public:
vector<int> findRightInterval(vector<Interval>& intervals)
{
int n=intervals.size();
vector<int> ans(n,-1);
vector<pair<Interval,int>> h;
for(int i=0;i<n;i++)
h.push_back(pair<Interval,int>(intervals[i],i));
sort(h.begin(),h.end(),cmp);
int i,j;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(h[j].first.start>=h[i].first.end)
{
ans[h[i].second]=h[j].second;
break;
}
}
}
return ans;
}
};