思路:区域覆盖问题。一个自然的想法是将每个员工的工作时间段看做一个木棒,每个木棒的长度就是这个时间段的时长。然后按照木棒的起始位置升序排列,接着由低位置向高位置一个木棒一个木棒的看过去。如果当前木棒的末节点的位置>下一个木棒的头节点位置,那么这两个节点就是一个free time的开头和结尾;如果当前木棒的末节点位置<=下一个木棒的头节点位置,那么更新当前木棒的末节点位置为max(当前木棒的末节点位置,下一个木棒的头节点位置)。

 /**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
class Solution {
public List<Interval> employeeFreeTime(List<List<Interval>> schedule) {
List<Interval> res = new ArrayList<>();
PriorityQueue<Interval> pq = new PriorityQueue<>((a,b)->a.start - b.start);//按照第一个元素升序排列
schedule.forEach(e->pq.addAll(e));//lamdba表达式,将schedule的每个元素的每个子元素加入pq中
Interval before = pq.poll();
while(!pq.isEmpty()) {
if(before.end < pq.peek().start) {
res.add(new Interval(before.end, pq.peek().start));
before = pq.poll();
}else{
before = before.end < pq.peek().end ? pq.peek() : before;//这里不能写成before = before.end < pq.peek().end ? pq.poll() : before;会Time Limit Exceeded
pq.poll();
}
}
return res;
}
}

Leetcode 759. Employee Free Time的更多相关文章

  1. LeetCode 690. Employee Importance (职员的重要值)

    You are given a data structure of employee information, which includes the employee's unique id, his ...

  2. [LeetCode]577. Employee Bonus 员工奖金

    Select all employee's name and bonus whose bonus is < 1000. Table:Employee +-------+--------+---- ...

  3. LeetCode - 690. Employee Importance

    You are given a data structure of employee information, which includes the employee's unique id, his ...

  4. (BFS) leetcode 690. Employee Importance

    690. Employee Importance Easy 377369FavoriteShare You are given a data structure of employee informa ...

  5. LeetCode 690 Employee Importance 解题报告

    题目要求 You are given a data structure of employee information, which includes the employee's unique id ...

  6. [LeetCode] 577. Employee Bonus_Easy tag: SQL

    Select all employee's name and bonus whose bonus is < 1000. Table:Employee +-------+--------+---- ...

  7. [LeetCode] 690. Employee Importance_Easy tag: BFS

    You are given a data structure of employee information, which includes the employee's unique id, his ...

  8. leetcode 690. Employee Importance——本质上就是tree的DFS和BFS

    You are given a data structure of employee information, which includes the employee's unique id, his ...

  9. LN : leetcode 690 Employee Importance

    lc 690 Employee Importance 690 Employee Importance You are given a data structure of employee inform ...

随机推荐

  1. Codeforces812A Sagheer and Crossroads 2017-06-02 20:41 139人阅读 评论(0) 收藏

    A. Sagheer and Crossroads time limit per test 1 second memory limit per test 256 megabytes input sta ...

  2. 谷歌的java文本差异对比工具

    package com.huawei.common.transfertool.utils; /** * @author Paul * @version 0.1 * @date 2018/12/11 * ...

  3. lis nlogn算法

    当前所在位的最长上升子序列只和前面一个字符有关 #include <iostream> #include <algorithm> using namespace std; ]; ...

  4. 用jquery监听输入数字的变化

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  5. 论EFMS模拟量部分采集电路的修改

    论1:电阻R11的作用 如图1是2014-3-11之前模拟量采集的部分硬件电路,图2是纠正后的正确电路. D5是SA20CA,TVS双向二极管,有效防止外接电源的浪涌冲击情况,保护电路.  D6是稳压 ...

  6. poj 1195 单点更新 区间求和

    Mobile phones Time Limit: 5000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Jav ...

  7. iOS 百度地图截屏

    关于百度地图截屏的问题,发现不能用常用的方法进行载屏,常用的截屏方法所得到的图片地图瓦片底图会显示空白,网上给出的答案是这样的 :因为百度地图不是用UIKit实现的,所以得不到截图! 不过通过Open ...

  8. Python 数据类型之一:列表(list)

    本次内容主要是总结一下 Python 数据类型中的 list (列表),关于 list 我在 Python 学习第二章已经简单介绍过了,这次呢,我这边主要总结自己学到的跟大家分享一下,有什么不对或者更 ...

  9. 关于css3中的flex

    参考几篇文章: Flex 布局语法教程 IE10中的Flexible Box("Flexbox")布局 “老”的Flexbox和“新”的Flexbox 一个可以练习的地方: NEW ...

  10. BZOJ2178: 圆的面积并(格林公式)

    题面 传送门 题解 好神仙-- 先给几个定义 平面单连通区域:设\(D\)是平面内一区域,若属于\(D\)内任一简单闭曲线的内部都属于\(D\),则称\(D\)为单连通区域.通俗地说,单连通区域是没有 ...