-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path757.cpp
More file actions
50 lines (41 loc) · 1.21 KB
/
757.cpp
File metadata and controls
50 lines (41 loc) · 1.21 KB
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
#include <bits/stdc++.h>
using namespace std;
int intersectionSizeTwo(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end(), [](auto &a, auto &b){
if (a[1] == b[1]) return a[0] > b[0];
return a[1] < b[1];
});
vector<int> nums;
int a = -1, b = -1;
for (auto &in : intervals) {
int start = in[0];
int end = in[1];
bool hasA = (a >= start && a <= end);
bool hasB = (b >= start && b <= end);
if (hasA && hasB) continue;
else if (hasB) {
int x = end;
nums.push_back(x);
a = b;
b = x;
}
else if (hasA) {
nums.push_back(end - 1);
nums.push_back(end);
b = end;
a = end - 1;
}
else {
nums.push_back(end - 1);
nums.push_back(end);
a = end - 1;
b = end;
}
}
return nums.size();
}
int main() {
vector<vector<int>> intervals = {{1, 3}, {1, 4}, {2, 5}, {3, 5}};
cout << intersectionSizeTwo(intervals) << endl;
return 0;
}