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 ...
随机推荐
- (01背包 先排序)Proud Merchants (hdu 3466)
http://acm.hdu.edu.cn/showproblem.php?pid=3466 Description Recently, iSea went to an ancient count ...
- python 开发学习
https://www.cnblogs.com/wj-1314/p/8476197.html
- lis nlogn算法
当前所在位的最长上升子序列只和前面一个字符有关 #include <iostream> #include <algorithm> using namespace std; ]; ...
- 使用PinYin4j.jar将汉字转换为拼音
package com.Test.util; import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j ...
- Dacapao 实验集(9.12 版本) 能不能给个网址?【内存分析实验】
网址 Dacapao 实验集 引用 以前看到的文章,如果使用这个基准程序,引用文献很多时候是一篇论文: Blackburn S M, Garner R, Hoffmann C, et al. The ...
- Spring Webservices(转)
17.5. Web Services Spring为标准Java web服务API提供了全面的支持: 使用JAX-RPC暴露web服务 使用JAX-RPC访问web服务 使用JAX-WS暴露web服务 ...
- devexpress gridview 添加按钮
#region 添加按钮事件 private RepositoryItemButtonEdit CreateRepositoryItemButtonEdit(Dictionary<object, ...
- 关于css中float的理解
感觉css里的float是个非常神奇的东西,神奇之处在于,你知道它是什么意思,但是用的时候总是不知道怎么实现效果.又或者它会很容易地影响到别的元素和属性.所以今天打算尝试一下float的各种设置,看看 ...
- Palindrome II
Problem Statement Given a string s, partition s such that every substring of the partition is a pali ...
- Android开发教程 - 使用Data Binding(四)在Fragment中的使用
本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...