LC 759. Employee Free Time 【lock, hard】
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.
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]]
(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 = 1, schedule[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.
Note:
scheduleandschedule[i]are lists with lengths in range[1, 50].0 <= schedule[i].start < schedule[i].end <= 10^8.
先把所有interval merge 然后找出补集。时间复杂度O(n*log(n))因为有排序这一个操作。
Runtime 60ms,beats 18.06% (看来有更好的做法)
class Solution {
public:
static bool cmp(Interval v1, Interval v2) {
if (v1.start != v2.start) return v1.start < v2.start;
return v1.end < v2.end;
}
vector<Interval> employeeFreeTime(vector<vector<Interval>>& schedule) {
vector<Interval> allemp;
vector<Interval> merged;
for (int i = ; i < schedule.size(); i++) {
for (int j = ; j < schedule[i].size(); j++) {
allemp.push_back(schedule[i][j]);
}
}
sort(allemp.begin(), allemp.end(), cmp);
int start = allemp[].start;
int end = allemp[].end;
for (auto v : allemp) {
if (v.start <= end) {
end = max(end, v.end);
}
else {
merged.push_back(Interval(start, end));
start = v.start;
end = v.end;
}
}
merged.push_back(Interval(start, end));
// for (auto v : merged) {
// cout << v.start << " " << v.end << endl;
// }
vector<Interval> freetime;
if (merged.size() == ) return freetime;
for (int i = ; i < merged.size() - ; i++) {
freetime.push_back(Interval(merged[i].end, merged[i+].start));
}
return freetime;
}
};
下面使用最小堆,
时间其实也是 n log(n)的。但runtime beats 99%
class Solution {
public:
static bool cmp(Interval v1, Interval v2) {
if (v1.start != v2.start) return v1.start < v2.start;
return v1.end < v2.end;
}
public:
vector<Interval> employeeFreeTime(vector<vector<Interval>>& schedule) {
vector<Interval> allemp;
vector<Interval> merged;
vector<Interval> v;
auto compare = [](Interval lhs, Interval rhs) {return lhs.start > rhs.start; };
priority_queue<Interval, vector<Interval>, decltype(compare)> q(compare);
for (auto s : schedule) {
for (auto e : s) q.push(e);
}
auto prev = q.top();
q.pop();
while (!q.empty()) {
auto current = q.top();
q.pop();
if (prev.end < current.start) {
v.push_back(Interval(prev.end, current.start));
prev = current;
}
else {
prev.end = current.end < prev.end ? prev.end : current.end;
}
}
return v;
}
};
LC 759. Employee Free Time 【lock, hard】的更多相关文章
- LC 499. The Maze III 【lock,hard】
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...
- LC 871. Minimum Number of Refueling Stops 【lock, hard】
A car travels from a starting position to a destination which is target miles east of the starting p ...
- LC 660. Remove 9 【lock, hard】
Start from integer 1, remove any integer that contains 9 such as 9, 19, 29... So now, you will have ...
- LC 656. Coin Path 【lock, Hard】
Given an array A (index starts at 1) consisting of N integers: A1, A2, ..., AN and an integer B. The ...
- LC 245. Shortest Word Distance III 【lock, medium】
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- LC 244. Shortest Word Distance II 【lock, Medium】
Design a class which receives a list of words in the constructor, and implements a method that takes ...
- LC 163. Missing Ranges 【lock, hard】
Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, up ...
- LC 683. K Empty Slots 【lock,hard】
There is a garden with N slots. In each slot, there is a flower. The N flowers will bloom one by one ...
- LC 727. Minimum Window Subsequence 【lock,hard】
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof ...
随机推荐
- Vivotek 摄像头远程栈溢出漏洞分析及利用
Vivotek 摄像头远程栈溢出漏洞分析及利用 近日,Vivotek 旗下多款摄像头被曝出远程未授权栈溢出漏洞,攻击者发送特定数据可导致摄像头进程崩溃. 漏洞作者@bashis 放出了可造成摄像头 C ...
- 建立一个能持续处理的C/S网络程序
程序流程图: 代码演示: 服务器端: #include<WinSock2.h> #include<Windows.h> #include<stdio.h> #inc ...
- Summer training #2
A:不管有没有负数 一顿操作之后肯定只有正数 又因为A=A-B 所以最大值是一直在减小的 所以一定有结果 B:..一开始以为求min操作数 WA了2发 直接求所有数的GCD如果所有数的GCD都不是1的 ...
- Python爬虫进阶之Scrapy框架安装配置
Python爬虫进阶之Scrapy框架安装配置 初级的爬虫我们利用urllib和urllib2库以及正则表达式就可以完成了,不过还有更加强大的工具,爬虫框架Scrapy,这安装过程也是煞费苦心哪,在此 ...
- c++使用初始化列表来初始化字段
#include<iostream> using namespace std; class Student1 { private: int _a; int _b; public: void ...
- Linux-Shell编程之创建shell脚本并执行
1.创建文件 touch myFirst.sh 2.编辑文件 vi myFirst.sh 点击键盘的字母 I 进行编辑,输入一下内容 #!/bin/bash # My First script ls ...
- beautifulsoap常用取节点方法
取某个class的元素 soup.find('div', {'class', 'description'}) 取某个属性的值 download_content.find('li').find('a') ...
- Swoole:PHP7安装Swoole的步骤
下载 swoole 首先下载swoole的源码包,这个操作很简单,没有太多说的.(没有wget:brew install wget--mac) wget -c https://github.com/s ...
- MessagePack Java Jackson 在不关闭输入流(input stream)的情况下反序列化多变量
com.fasterxml.jackson.databind.ObjectMapper 在读取输入流变量的时候默认的将会关闭输入流. 如果你不希望关闭输入流,你可以设置 JsonParser.Feat ...
- 「美团 CodeM 初赛 Round A」试题泛做
最长树链 树形DP.我们发现gcd是多少其实并不重要,只要不是1就好了,此外只要有一个公共的质数就好了.计f[i][j]表示i子树内含有j因子的最长链是多少.因为一个数的不同的质因子个数是log级别的 ...