Comment on page

# 759.Employee-Free-Time

## 题目描述

We are given a list schedule of employees, which represents the working time for each employee.
Each employee has a list of non-overlapping Intervals, and these intervals are in sorted order.
Return the list of finite intervals representing common, positive-length free time for all employees, also in sorted order.
(Even though we are representing Intervals in the form [x, y], the objects inside are Intervals, not lists or arrays. For example, schedule.start = 1, schedule.end = 2, and schedule is not defined). Also, we wouldn't include intervals like [5, 5] in our answer, as they have zero length.
Example 1:
Input: schedule = [[[1,2],[5,6]],[[1,3]],[[4,10]]]
Output: [[3,4]]
Explanation: There are a total of three employees, and all common
free time intervals would be [-inf, 1], [3, 4], [10, inf].
We discard any intervals that contain inf as they aren't finite.
Example 2:
Input: schedule = [[[1,3],[6,7]],[[2,4]],[[2,5],[9,12]]]
Output: [[5,6],[7,9]]
Constraints:
1 <= schedule.length , schedule[i].length <= 50
0 <= schedule[i].start < schedule[i].end <= 10^8

## 代码

Approach 1: Events (Line Sweep)
Complexity Analysis
• Time Complexity: O_(_C_log_C), where C is the number of intervals across all employees.
• Space Complexity: O_(_C).
/*
// Definition for an Interval.
class Interval {
public int start;
public int end;
public Interval() {}
public Interval(int _start, int _end) {
start = _start;
end = _end;
}
};
*/
class Solution {
public List<Interval> employeeFreeTime(List<List<Interval>> schedule) {
int OPEN = 0, CLOSE = 1;
List<int[]> events = new ArrayList();
for (List<Interval> employee : schedule) {
for (Interval iv : employee) {
}
}
Collections.sort(events, (a, b) -> a != b ? a - b : a - b);
List<Interval> ans = new ArrayList();
int prev = -1, bal = 0;
for (int[] event : events) {
if (bal == 0 && prev >= 0) {
}
bal += (event == OPEN ? 1 : -1);
prev = event;
}
return ans;
}
}
Approach #2 Priority Queue
Complexity Analysis
• Time Complexity: O(_C_log_N), where N is the number of employees, and _C is the number of jobs across all employees. The maximum size of the heap is N, so each push and pop operation is O(log_N), and there are O(_C) such operations.
• Space Complexity: O_(_N) in additional space complexity.
class Solution {
public List<Interval> employeeFreeTime(List<List<Interval>> avails) {
List<Interval> ans = new ArrayList();
PriorityQueue<Job> pq = new PriorityQueue<Job>((a, b) ->
avails.get(a.eid).get(a.index).start -
avails.get(b.eid).get(b.index).start);
int ei = 0, anchor = Integer.MAX_VALUE;
for (List<Interval> employee: avails) {
pq.offer(new Job(ei++, 0));
anchor = Math.min(anchor, employee.get(0).start);
}
while (!pq.isEmpty()) {
Job job = pq.poll();
Interval iv = avails.get(job.eid).get(job.index);
if (anchor < iv.start)
anchor = Math.max(anchor, iv.end);
if (++job.index < avails.get(job.eid).size())
pq.offer(job);
}
return ans;
}
}
class Job {
int eid, index;
Job(int e, int i) {
eid = e;
index = i;
}
}