-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path759.cpp
42 lines (38 loc) · 1002 Bytes
/
759.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
/*
// Definition for an Interval.
class Interval {
public:
int start;
int end;
Interval() {}
Interval(int _start, int _end) {
start = _start;
end = _end;
}
};
*/
class Solution {
public:
vector<Interval> employeeFreeTime(vector<vector<Interval>> schedule) {
map<int, int> mp;
for (auto oneSchedule : schedule) {
for (auto interval : oneSchedule) {
mp[interval.start]++;
mp[interval.end]--;
}
}
vector<Interval> res;
int count = 0;
for (auto element : mp) {
count += element.second;
if (count == 0) {
Interval interval(element.first, 0);
res.push_back(interval);
}
if (count != 0 && res.size() > 0 && res.back().end == 0) res.back().end = element.first;
}
// remove the last one
if (res.size() > 0) res.pop_back();
return res;
}
};