forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet Intersection Size At Least Two.cpp
53 lines (39 loc) · 1.09 KB
/
Set Intersection Size At Least Two.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
// Runtime: 56 ms (Top 88.46%) | Memory: 17.8 MB (Top 31.41%)
class Solution {
public:
// sort wrt. end value
static bool compare(vector<int>& a, vector<int>& b)
{
if(a[1] == b[1])
return a[0] < b[0];
else
return a[1] < b[1];
}
int intersectionSizeTwo(vector<vector<int>>& intervals) {
int n = intervals.size();
// sort the array
sort(intervals.begin(), intervals.end(), compare);
vector<int> res;
res.push_back(intervals[0][1] - 1);
res.push_back(intervals[0][1]);
for(int i = 1; i < n; i++)
{
int start = intervals[i][0];
int end = intervals[i][1];
if(start > res.back())
{
res.push_back(end - 1);
res.push_back(end);
}
else if(start == res.back())
{
res.push_back(end);
}
else if(start > res[res.size() - 2])
{
res.push_back(end);
}
}
return res.size();
}
};