Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required.

Example 1:

Input: [[0, 30],[5, 10],[15, 20]]
Output: 2

Example 2:

Input: [[7,10],[2,4]]
Output: 1

NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.

这道题是之前那道 Meeting Rooms 的拓展,那道题只问我们是否能参加所有的会,也就是看会议之间有没有时间冲突,而这道题让求最少需要安排几个会议室,有时间冲突的肯定需要安排在不同的会议室。这道题有好几种解法,先来看使用 TreeMap 来做的,遍历时间区间,对于起始时间,映射值自增1,对于结束时间,映射值自减1,然后定义结果变量 res,和房间数 rooms,遍历 TreeMap,时间从小到大,房间数每次加上映射值,然后更新结果 res,遇到起始时间,映射是正数,则房间数会增加,如果一个时间是一个会议的结束时间,也是另一个会议的开始时间,则映射值先减后加仍为0,并不用分配新的房间,而结束时间的映射值为负数更不会增加房间数,利用这种思路可以写出代码如下:

解法一:

class Solution {
public:
int minMeetingRooms(vector<vector<int>>& intervals) {
map<int, int> m;
for (auto a : intervals) {
++m[a[]];
--m[a[]];
}
int rooms = , res = ;
for (auto it : m) {
res = max(res, rooms += it.second);
}
return res;
}
};

第二种方法是用两个一维数组来做,分别保存起始时间和结束时间,然后各自排个序,定义结果变量 res 和结束时间指针 endpos,然后开始遍历,如果当前起始时间小于结束时间指针的时间,则结果自增1,反之结束时间指针自增1,这样可以找出重叠的时间段,从而安排新的会议室,参见代码如下:

解法二:

class Solution {
public:
int minMeetingRooms(vector<vector<int>>& intervals) {
vector<int> starts, ends;
int res = , endpos = ;
for (auto a : intervals) {
starts.push_back(a[]);
ends.push_back(a[]);
}
sort(starts.begin(), starts.end());
sort(ends.begin(), ends.end());
for (int i = ; i < intervals.size(); ++i) {
if (starts[i] < ends[endpos]) ++res;
else ++endpos;
}
return res;
}
};

再来一看一种使用最小堆来解题的方法,这种方法先把所有的时间区间按照起始时间排序,然后新建一个最小堆,开始遍历时间区间,如果堆不为空,且首元素小于等于当前区间的起始时间,去掉堆中的首元素,把当前区间的结束时间压入堆,由于最小堆是小的在前面,那么假如首元素小于等于起始时间,说明上一个会议已经结束,可以用该会议室开始下一个会议了,所以不用分配新的会议室,遍历完成后堆中元素的个数即为需要的会议室的个数,参见代码如下;

解法三:

class Solution {
public:
int minMeetingRooms(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>& b){ return a[] < b[]; });
priority_queue<int, vector<int>, greater<int>> q;
for (auto interval : intervals) {
if (!q.empty() && q.top() <= interval[]) q.pop();
q.push(interval[]);
}
return q.size();
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/253

类似题目:

Merge Intervals

Meeting Rooms

参考资料:

https://leetcode.com/problems/meeting-rooms-ii/

https://leetcode.com/problems/meeting-rooms-ii/discuss/67857/AC-Java-solution-using-min-heap

https://leetcode.com/problems/meeting-rooms-ii/discuss/67883/Super-Easy-Java-Solution-Beats-98.8

https://leetcode.com/problems/meeting-rooms-ii/discuss/67996/C%2B%2B-O(n-log-n)-584%2B-ms-3-solutions

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 253. Meeting Rooms II 会议室之二的更多相关文章

  1. [LeetCode] 253. Meeting Rooms II 会议室 II

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  2. [leetcode]253. Meeting Rooms II 会议室II

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  3. [LeetCode] Meeting Rooms II 会议室之二

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  4. [LeetCode#253] Meeting Rooms II

    Problem: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2] ...

  5. 253. Meeting Rooms II 需要多少间会议室

    [抄题]: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],.. ...

  6. 【LeetCode】253. Meeting Rooms II 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序+堆 日期 题目地址:https://leetco ...

  7. 253. Meeting Rooms II

    题目: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] ...

  8. LeetCode 252. Meeting Rooms (会议室)$

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  9. [LC] 253. Meeting Rooms II

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

随机推荐

  1. 通过requirements.txt文件创建虚拟环境副本

    1.首先在原来的环境中生成一个需求文件requirements.txt,用于记录所有依赖包及其精确的版本号. (venv) $ pip freeze >requirements.txt 2.创建 ...

  2. LeetCode 189:旋转数组 Rotate Array

    公众号:爱写bug(ID:icodebugs) 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. Given an array, rotate the array to the ...

  3. Leetcode练习题Two Sum

    1 Two Sum: Question Solution 知识点总结 常见方法 HashMap由value获得key Question: Given an array of integers, ret ...

  4. 明解C语言 入门篇 第十二章答案

    练习12-1 /* 用表示学生的结构体来显示高尾的信息 */ #include <stdio.h> #define NAME_LEN 64 /* 姓名的字符数 */ /*=== 表示学生的 ...

  5. kali渗透综合靶机(十七)--HackInOS靶机

    kali渗透综合靶机(十七)--HackInOS靶机 靶机下载地址:https://www.vulnhub.com/hackinos/HackInOS.ova 一.主机发现 1.netdiscover ...

  6. WPF绑定 mode Using System.ComponentModel; IPropertyChanged, if(this.PropertyChanged!=null){ this.PropertyChanged.Invoke(this,new PropertyChangedEventArgs("Name"))

    Mode,它的类型为BindingMode的枚举类型,可以取TwoWay.OneWay.OnTime.OneWayToSource.Default. oneWay:使用 OneWay 绑定时,每当源发 ...

  7. ASP.NET Core launchsettings.json 文件

    ASP.NET Core launchsettings.json 文件 在本节中,我们将讨论在 ASP.NET Core 项目中launchsettings.json文件的重要性. launchset ...

  8. 关于 L3 缓存行 cacheLIne 的研究!还是对程序有举足轻重的作用!

    https://www.cnblogs.com/PurpleTide/archive/2010/11/25/1887506.html CLR via C# 读书笔记 2-3 Cache Lines a ...

  9. JavaScript 设计模式分类

    设计模式的目的是为了提高代码的整洁性.降低代码的资源占用量. JS中的设计模式可分为以下三种: 1. 创建型设计模式 说明:专注于处理对象创建的机制,以合适的方式创建对象,以此来降低创建对象过程的复杂 ...

  10. SpirngBoot整合Spring-data-JPA

    0.引言 使用SpringBoot data jpa技术相比mybatis是比较难的,这里只给出整合方法 1.引入SpringBoot data jpa <!--JPA依赖--> < ...