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:

  1. schedule and schedule[i] are lists with lengths in range [1, 50].
  2. 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】的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. LC 163. Missing Ranges 【lock, hard】

    Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, up ...

  8. 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 ...

  9. 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  ...

随机推荐

  1. C语言-数字字符串转换成这个字符串对应的数字(十进制、十六进制)

    数字字符串转换成这个字符串对应的数字(十进制.十六进制) (1)数字字符串转换成这个字符串对应的数字(十进制) 要求:这个字符串参数必须包含一个或者多个数字,函数应该把这些数字转换为整数并且返回这个整 ...

  2. CSS——简写属性(在padding和margin这样的简写属性中,值赋值的顺序是top、right、bottom、left)

    /* 在padding和margin这样的简写属性中,值赋值的顺序是top.right.bottom.left. 它们还有其他简写方式,例如给padding两个值,则第一个值表示top/bottom, ...

  3. python 2 和python 3 中的编码对比

    在 Python 中,不论是 Python2 还是 Python3 中,总体上说,字符都只有两大类: 通用的 Unicode 字符: (unicode 被编码后的)某种编码类型的字符,比如 UTF-8 ...

  4. 通过CSS实现 文字渐变色 的两种方式

    说明 这次的重点就在于两个属性, background 属性 mask 属性这两个属性分别是两种实现方式的关键. 方式一 解释 <!DOCTYPE html> <html> & ...

  5. 使用python脚本进行数据清洗(1)

    1. 原始表 CREATE TABLE ml_100k (userid INT, movieid INT, rating INT, unixtime STRING) ROW FORMAT DELIMI ...

  6. HDU-2072-单词数(字典树)

    链接: https://vjudge.net/problem/HDU-2072 题意: lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数.下面 ...

  7. 02 Vue介绍与安装,指令系统 v-*、音乐播放器

    VUE文档 https://cn.vuejs.org/v2/guide/ 1.vue的介绍 尤雨溪 1.vue的优点 2.vue的介绍 3.vue的安装 4.声明式渲染 <body> &l ...

  8. 题解 【ZJOI2009】 假期的宿舍

    题面 解析 这其实就是个二分图匹配的水题(虽然我还是爆零了) 这题的意思就是说,有x个人,y张床(x,y不确定), 每个人只能睡在指定的几张床上, 问能否使每人都有床睡. 所以,直接二分图匹配,看最大 ...

  9. Python—数据类型之字典(Dict)

    其它数据类型转成字典 arr1 = ['jack', 'rose', 'marry'] arr2 = [68, 85, 66] dict1 = dict(zip(arr1, arr2)) print( ...

  10. BZOJ 1406: [AHOI2007]密码箱 exgcd+唯一分解定理

    推出来了一个解法,但是感觉复杂度十分玄学,没想到秒过~ Code: #include <bits/stdc++.h> #define ll long long #define N 5000 ...