Leetcode 759. Employee Free Time

思路:区域覆盖问题。一个自然的想法是将每个员工的工作时间段看做一个木棒,每个木棒的长度就是这个时间段的时长。然后按照木棒的起始位置升序排列,接着由低位置向高位置一个木棒一个木棒的看过去。如果当前木棒的末节点的位置>下一个木棒的头节点位置,那么这两个节点就是一个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的更多相关文章
- LeetCode 690. Employee Importance (职员的重要值)
You are given a data structure of employee information, which includes the employee's unique id, his ...
- [LeetCode]577. Employee Bonus 员工奖金
Select all employee's name and bonus whose bonus is < 1000. Table:Employee +-------+--------+---- ...
- LeetCode - 690. Employee Importance
You are given a data structure of employee information, which includes the employee's unique id, his ...
- (BFS) leetcode 690. Employee Importance
690. Employee Importance Easy 377369FavoriteShare You are given a data structure of employee informa ...
- LeetCode 690 Employee Importance 解题报告
题目要求 You are given a data structure of employee information, which includes the employee's unique id ...
- [LeetCode] 577. Employee Bonus_Easy tag: SQL
Select all employee's name and bonus whose bonus is < 1000. Table:Employee +-------+--------+---- ...
- [LeetCode] 690. Employee Importance_Easy tag: BFS
You are given a data structure of employee information, which includes the employee's unique id, his ...
- leetcode 690. Employee Importance——本质上就是tree的DFS和BFS
You are given a data structure of employee information, which includes the employee's unique id, his ...
- LN : leetcode 690 Employee Importance
lc 690 Employee Importance 690 Employee Importance You are given a data structure of employee inform ...
随机推荐
- kepware http接口 php
读取某变量的值(HttpRequest <?php $request = new HttpRequest(); $request->setUrl('http://127.0.0.1:393 ...
- java基础-day30
第07天 MySQL数据库 今日内容介绍 u 多表关系实战练习 u 多表查询 u SQL语句的练习 第1章 多表关系实战练习 1.1 多表关系--实战1--省和市 1.1.1 需求分析 在数据库 ...
- CI、CD和dev-ops概念
传统的开发方式是:需求方提供文档,实现方按照文档一步步开发,中间很少变动和修改. 但是随着市场的变化,产品更新迭代的加快,也要求开放方更快的响应变化,用最短的时间开发,部署上线. 这样,持续集成(CI ...
- HTML 基础 块级元素与行内元素
块元素:单独占一行,宽度占整行,可以包含内联元素和其他块元素,通过样式display:inline,变为行内元素,不换行 内联元素:不单独占一行,宽度根据内容来决定,只能容纳文本或者其他内联元素 ,可 ...
- hdu 1.2.4
采用异或... #include<stdio.h> int main() { //freopen("input.txt","r",stdin); i ...
- [转载]腾讯专家:论高级DBA的自我修养
作者介绍: 张秀云:2007年开始从事运维方面的工作,经历过网络管理员.linux运维工程师.DBA.分布式存储运维等多个IT职位.对linux运维.mysql数据库.分布式存储有丰富的经验.2012 ...
- nginx 常见错误释义
错误信息 错误说明 "upstream prematurely(过早的) closed connection" 请求uri的时候出现的异常,是由于upstream还未返回应答给用户 ...
- MySQL1安装
---恢复内容开始--- (1)安装MySQL ①ZIP安装 ②MSI安装(Windows Installer) Ⅰ 先从官网下载 目前选择使用Typical安装↑ 目前不勾选配置选项↑ 找到配置向 ...
- XAML 调试工具 不见了?
XAML调试工具不见了怎么办? 1.调试---> 选项---> 选中 启用XAML的UI调试工具 2.调试---> 选项---> 禁用 使用托管兼容模式 欧了!
- Java中goto和break、continue实现区别
goto 关键字很早就在程序设计语言中出现.事实上,goto 是汇编语言的程序控制结构的始祖:“若条件 A,则跳到这里:否则跳到那里”.若阅读由几乎所有编译器生成的汇编代码,就会发现程序控制里包含了许 ...