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 ...
随机推荐
- MySQL 安装与基本概念
Mysql版本 第一条产品线:5.0.xx及升级到5.1.xx的产品系列,这条产品线继续完善与改进其用户体验和性能,同时增加新功能,这条路线可以说是MySQL早期产品的延续系列,这一系列的产品发布情况 ...
- 四、指定Nginx启动用户
一.nginx指定启动用户 1.参考宝塔的配置 解释:(linux权限管理) 指定用www用户启动nginx,如果你用root启动nginx,万一nginx有漏洞,被提权了,你服务器就GG了 所以指定 ...
- python面向编程:阶段练习
1.所有程序都因该使用面向对象来设计吗?为什么? 不是,面向对象编程优点是扩展性高,对程序员来说不需要关心具体的步骤,只需要调用对象功能,缺点是:程序的复杂度变高,整体的可控性比较低! 2.什么是对象 ...
- 一次完整的HTTP请求所经历的7个步骤【转】
HTTP通信机制是在一次完整的HTTP通信过程中,Web浏览器与Web服务器之间将完成下列7个步骤: 1. 建立TCP连接 在HTTP工作开始之前,Web浏览器首先要通过网络与Web服务器建立连接,该 ...
- Android及java中list循环添加时覆盖的问题-20171021
鉴于新浪博客太渣,转到这来. 最近在工程设计时,使用list循环添加map对象发现,最终全部变为最后一个map的值,但是list的数值还是正确的,也就是说添加了N(list长度或者说循环的次数)个相同 ...
- es实战之数据导出成csv文件
从es将数据导出分两步: 查询大量数据 将数据生成文件并下载 本篇主要是将第二步,第一步在<es实战之查询大量数据>中已讲述. csv vs excel excel2003不能超过6553 ...
- hive的事物性 transaction manager
create table lk3 (id string,nname string,grade int,goldUser int); insert into lk3 values (,, ), (,, ...
- [Git] How to revert one file changes from one commit
Many times we might changed one file which we don't intent to do... but it was too late, until we fo ...
- 小程序对于华为Oppo的canvas二维码渲染数据量大
setTimeout(()=>{ ctx.draw(false, function (e) { options.callback && options.callback(e); ...
- BZOJ 3667: Rabin-Miller算法 (Pollard-Rho 模板)
说实话,我知道每一步都干啥,但我完全不知道为啥这么做,也不知道为什么是正确的,反正会用就行了~ #include <cmath> #include <cstdio> #incl ...