[LeetCode] 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[0][0].start = 1schedule[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

员工空闲时间。

给定员工的 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]]

(尽管我们用 [x, y] 的形式表示 Intervals ,内部的对象是 Intervals 而不是列表或数组。例如,schedule[0][0].start = 1, schedule[0][0].end = 2,并且 schedule[0][0][0] 是未定义的)

而且,答案中不包含 [5, 5] ,因为长度为 0。

注:

schedule 和 schedule[i] 为长度范围在 [1, 50]的列表。
0 <= schedule[i].start < schedule[i].end <= 10^8。
注:输入类型于 2019 年 4 月 15 日 改变。请重置为默认代码的定义以获取新方法。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/employee-free-time
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路还是扫描线算法。但是这里有一些细节需要解释一下,input 给的是一个二维list,是把所有员工的 schedule 混在一起,以interval of intervals 表示。这个题有两种思路做,一种是以 star time 排序的最小堆,一种是先排序再用扫描线的方法做。无论哪种做法,都需要把二维 list 先转成一维。

pq 的做法是,将所有人的所有 interval 加入一个以 start time 排序的 pq,先弹出一个,记为 temp,比较 temp.end 和 pq.peek().start 的大小关系。若两者没有交集,则发现了一个gap,将其加入结果集;若没有交集,则 temp.end = Math.max(temp.end, pq.peek().end)。

时间O(nlogn)

空间O(n)

Java实现ONLY

 1 class Solution {
 2     public List<Interval> employeeFreeTime(List<List<Interval>> schedule) {
 3         List<Interval> res = new ArrayList<>();
 4         PriorityQueue<Interval> pq = new PriorityQueue<>((a, b) -> a.start - b.start);
 5         schedule.forEach(e -> pq.addAll(e));
 6         Interval temp = pq.poll();
 7         while (!pq.isEmpty()) {
 8             if (temp.end < pq.peek().start) {
 9                 res.add(new Interval(temp.end, pq.peek().start));
10                 temp = pq.poll();
11             } else {
12                 temp = temp.end < pq.peek().end ? pq.peek() : temp;
13                 pq.poll();
14             }
15         }
16         return res;
17     }
18 }

 

扫描的做法非常类似。将所有人的所有 interval 放在一起,按 start time 排序。接着两两比较,看是否前一个 interval.end < 后一个interval.start,若是则找到一个 gap,加入结果集;若不是则接着往后找。

时间O(nlogn)

空间O(n)

Java实现

 1 class Solution {
 2     public List<Interval> employeeFreeTime(List<List<Interval>> schedule) {
 3         List<Interval> res = new ArrayList<>();
 4         List<Interval> copy = new ArrayList<>();
 5         schedule.forEach(e -> copy.addAll(e));
 6         Collections.sort(copy, (a, b) -> a.start - b.start);
 7         Interval temp = copy.get(0);
 8         for (Interval each : copy) {
 9             if (temp.end < each.start) {
10                 res.add(new Interval(temp.end, each.start));
11                 temp = each;
12             } else {
13                 temp = temp.end < each.end ? each : temp;
14             }
15         }
16         return res;
17     }
18 }

 

JavaScript实现

 1 /**
 2  * @param {Interval[][]} schedule
 3  * @return {Interval[]}
 4  */
 5 var employeeFreeTime = function(schedule) {
 6     let res = [];
 7     let copy = [];
 8     for (let employee of schedule) {
 9         for (let interval of employee) {
10             copy.push(interval);
11         }
12     }
13     copy.sort((a, b) => a.start - b.start);
14     let temp = copy[0];
15     for (each of copy) {
16         if (temp.end < each.start) {
17             res.push(new Interval(temp.end, each.start));
18             temp = each;
19         } else {
20             temp = temp.end < each.end ? each : temp;
21         }
22     }
23     return res;
24 };

 

扫描线相关题目

LeetCode 题目总结

posted @ 2020-04-10 14:27  CNoodle  阅读(640)  评论(0编辑  收藏  举报