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. Java学习笔记【八、数据结构】

    参考资料: http://www.cnblogs.com/janneystory/p/5758958.html array arraylist list linklist的区别 http://www. ...

  2. 微信小程序开发(二)创建一个小程序页面

    为了方便讲解,我们将上篇博客创建的小程序除了project.config.json和sitemap.json两个文件保留,其他全部删除(这两个文件存的是小程序的创建信息,删掉会有报错提示). 接下来我 ...

  3. NTP时间同步服务和DNS服务

    NTP服务是搭建集群的第一步,是保持时间的同步性,保持一致 服务端 首先下载 yum install ntp –y 而后打开配置文件 /etc/ntp.conf 配置文件里有很多内容,但只要留三行就足 ...

  4. 修改虚拟机CentOS系统ip地址和主机名

    按照教程安装了虚拟机但是未配置静态IP,所以导致IP地址经常变化,CRT,mysql等连接时经常出现问题. 所以修改虚拟机内CentOS系统的IP为静态IP. 一.查看当前网关 虚拟机-->[编 ...

  5. 部署jenkins+git

    Jenkins简介 Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能 安装并启动思路: 安装准备 ...

  6. phpStudy环境下composer的安装

    前言 原来是做php开发的,现在转行前端工程师,因为很久没有接触了,可能会有其他问题,这里简单记录一下,后期遇到什么问题再进行更新~ 话说下载特别慢所以这里给个网盘链接Composer-Setup.e ...

  7. hive日期函数-广发实战(三)

    近一月客户新增常规里程数与额度比即上个月 第一天(包含)到上个月最后一天(包含) 字段是batch_date==>格式是 yyyymmdd ),'MM'),'-',''); +--------- ...

  8. 【Winfrom-禁止重复启动程序】 程序不能重复启动

    using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Sy ...

  9. [Python之路] 多种方式实现并发Web Server

    下面我们使用Python来实现并发的Web Server,其中采用了多进程.多线程.协程.单进程单线程非阻塞的方式. 一.使用子进程来实现并发Web Server 参照 https://www.cnb ...

  10. 如何将vs2015中的英文注释改为中文

    找一个中文版的vs 将 C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework        文件夹 ...