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[0][0].start = 1, schedule[0][0].end = 2, and schedule[0][0][0] 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; }};*/classSolution {publicList<Interval> employeeFreeTime(List<List<Interval>> schedule) {int OPEN =0, CLOSE =1;List<int[]> events =newArrayList();for (List<Interval> employee : schedule) {for (Interval iv : employee) {events.add(newint[] {iv.start, OPEN});events.add(newint[] {iv.end, CLOSE}); } }Collections.sort(events, (a, b) -> a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]);List<Interval> ans =newArrayList();int prev =-1, bal =0;for (int[] event : events) {if (bal ==0&& prev >=0) {ans.add(newInterval(prev, event[0])); } bal += (event[1] == OPEN ?1:-1); prev = event[0]; }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.
classSolution {publicList<Interval> employeeFreeTime(List<List<Interval>> avails) {List<Interval> ans =newArrayList();PriorityQueue<Job> pq =newPriorityQueue<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(newJob(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)ans.add(newInterval(anchor,iv.start)); anchor =Math.max(anchor,iv.end);if (++job.index<avails.get(job.eid).size())pq.offer(job); }return ans; }}classJob {int eid, index;Job(int e,int i) { eid = e; index = i; }}