[LeetCode] 253. Meeting Rooms II 会议室之二
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
类似题目:
参考资料:
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
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 253. Meeting Rooms II 会议室之二的更多相关文章
- [LeetCode] 253. Meeting Rooms II 会议室 II
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- [leetcode]253. Meeting Rooms II 会议室II
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- [LeetCode] Meeting Rooms II 会议室之二
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- [LeetCode#253] Meeting Rooms II
Problem: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2] ...
- 253. Meeting Rooms II 需要多少间会议室
[抄题]: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],.. ...
- 【LeetCode】253. Meeting Rooms II 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序+堆 日期 题目地址:https://leetco ...
- 253. Meeting Rooms II
题目: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] ...
- LeetCode 252. Meeting Rooms (会议室)$
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- [LC] 253. Meeting Rooms II
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
随机推荐
- ASCll编码,
- RHCE实验记录总结-1-RHCSA
不管是运维还是开发系统的了解下Linux或者系统的温习整理一下Linux知识点无疑是较好的,这篇文章是对RHCSA&RHCE实验进行一个汇总,是我为了做实验方便(并分享给朋友)的一篇文章. 前 ...
- 『月之谜 数位dp』
月之谜 Description 打败了Lord lsp 之后,由 于lqr 是一个心地善良的女孩 子,她想净化Lord lsp 黑化的 心,使他变回到原来那个天然 呆的lsp--在倒霉的光之英 雄ap ...
- S-T-E-A-M Science Technology Engineering Art Mathematics 五种思维模式
S-T-E-A-M五个英文字母分别代表 Science 科学,Technology 技术,Engineering 工程,Art 艺术以及 Mathematics 数学.它们并不是简单地整合原来的分科体 ...
- 转 让FPGA替代GPU的6大顾虑,你确定不看看吗?
最近FPGA又频频被各AI领域的巨头看好,比如微软.百度.科大讯飞都对FPGA应用前景有所期待.那么如果让你选择FPGA作为AI计算系统的主力军,你会有什么样的顾虑? 这几天,已经退役的AlphaGo ...
- Visual Studio2017使用EF添加Mysql
为了能够在Visual Studio 中集成Mysql, 首先需要安装MySql的连接工具 与 MySql的VisualStudio插件. MySQL Connector Net 6.8.8 (目前最 ...
- javascript:警告(alert 消息对话框),确认(confirm 消息对话框)
原文:https://blog.csdn.net/u012110719/article/details/41926315
- JavaScript RegExp(正则表达式) 对象
正则表达式是描述字符模式的对象.正则表达式用于在文本上执行模式匹配和“搜索和替换”功能. var patt = /JC2182/i 示例说明: /JC2182/i - 是一个正则表达式. JC2182 ...
- 云顶之弈换中立python脚本
import pynput keyboard = pynput.keyboard.Controller() mouse = pynput.mouse.Controller() def on_relea ...
- 使用Composer安装阿里云短信失败
安装步骤 请参考以下步骤,使用Composer安装依赖. 如果在您的系统上全局安装Composer,您可以在项目目录中运行以下内容,将 Alibaba Cloud Client for PHP 添加为 ...