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程式的linux命令 启动mysqld 检查端口是否运行 查看数据库程式相关信息 查看mysql版本 查看配置文件位置 登陆mysql 修改密码 SQL命令 查看数据库 ...
- go语言json转map
package util import ( "encoding/json" "fmt" ) // json转map函数,通用 func JSONToMap(st ...
- linux常用命令(centos)
linux 命令有很多,常用的很少. #######################系统相关############################ lsb_release -a 查看系统信息 cat ...
- bootstrap和JS实现下拉菜单
// bootstrap下拉菜单 <div class="btn-group"> <button id="button_text" type= ...
- C++——函数
C++基础函数 (一)函数的参数传递 在没有调用函数之前,函数的形参并没有占据实际的空间. 1.值传递 传入的仅仅只是一个值--就是把实参的值赋给形参.形参自己会在内存中开辟一个空间! 2.传引用 这 ...
- QTP(2)
注意: 在使用QTP录制代码时,能使用鼠标点击的就不要使用键盘操作,能单击的操作就不要使用双击 一.QTP的工作流程 1.录制测试脚本前的准备: a.分析被测系统是否可以实现自动化测试 b.分析被测系 ...
- da面板修改SSH端口号
进入da面板,找到管理工具菜单下的文件编辑器,点击进入,选择所要编辑的文件/etc/ssh/sshd_config 点击右侧的显示文件,即可打开该文件进行编辑,例如可以将原始端口22修改为 33 #P ...
- JavaScript设计模式与开发实践(二)——apply&&call
call和apply的用途 改变this指向 先看个例子: var obj1 = { name: 'sven' }; var obj2 = { name: 'anne' }; window.name ...
- TCP协议(包括TCP的连接过程,数据分段,TCP有关服务器优化)
Transmission Control Protocol/Internet Protocol 传输控制协议/因特网互联协议 TCP/IP是一个Protocol Stack(协议栈),包括TCP.IP ...
- ESP8266_04管脚控制与软件定时器
ESP8266_04管脚控制与软件定时器 from :https://mp.weixin.qq.com/s/APawDx4io4gKJyOeuErTLA 原创: MCU启航 单片机爱好者 今天 这一节 ...