
给定员工的 schedule 列表,表示每个员工的工作时间。
每个员工都有一个非重叠的时间段 Intervals 列表,这些时间段已经排好序。
返回表示 所有 员工的 共同,正数长度的空闲时间 的有限时间段的列表,同样需要排好序。
示例 1:
输入:schedule = [[[1,2],[5,6]],[[1,3]],[[4,10]]]
输出:[[3,4]]
解释:
共有 3 个员工,并且所有共同的
空间时间段是 [-inf, 1], [3, 4], [10, inf]。
我们去除所有包含 inf 的时间段,因为它们不是有限的时间段。
示例 2:
输入:schedule = [[[1,3],[6,7]],[[2,4]],[[2,5],[9,12]]]
输出:[[5,6],[7,9]]
而且,答案中不包含 [5, 5] ,因为长度为 0。
schedule 和 schedule[i] 为长度范围在 [1, 50]的列表。
0 <= schedule[i].start < schedule[i].end <= 10^8。来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/employee-free-time 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
/*
// 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) {
int l = INT_MAX;
vector<Interval> v;
for(auto& s: schedule)
for(auto& i : s)
{
v.push_back(i);
l = min(l, i.end);
}
sort(v.begin(), v.end(), [&](auto& a, auto& b){
if(a.start == b.start)
return a.end < b.end;
return a.start < b.start;
});
vector<Interval> ans;
for(int i = 0; i < v.size(); ++i)
{
if(l < v[i].start)
ans.push_back(Interval(l, v[i].start));
l = max(l, v[i].end);
}
return ans;
}
};56 ms 10.6 MB