• [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列表,这些时间段已经排好序。请返回表示所有员工的共同的,正数长度的空闲时间的列表,同样需要排好序。

    思路还是扫描线算法。但是这里有一些细节需要解释一下,input给的是多个员工的schedule,以interval of intervals表示。有的员工的schedule是断开的,所以input里面会有一个员工多个interval的情况。这个题有两种思路做,一种是pq,一种是先排序再扫描。

    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 题目总结

  • 相关阅读:
    nginx配置文件中的location中文详解
    binlog、redo log、undo log区别
    OLTP和OLAP的区别
    MPP、SMP、NUMA概念介绍
    NUMA体系结构介绍
    在Linux下判断系统当前是否开启了超线程
    NUMA的取舍与优化设置
    LRU缓存算法
    optimize table
    使用innodb_force_recovery解决MySQL崩溃无法重启问题
  • 原文地址:https://www.cnblogs.com/cnoodle/p/12673290.html
Copyright © 2020-2023  润新知