【LeetCode】253. Meeting Rooms II 解题报告(C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.com/problems/meeting-rooms/
题目描述
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
题目大意
给定一个会议时间安排的数组,每个会议时间都会包括开始和结束的时间 [[s1,e1],[s2,e2],…] (si < ei),为避免会议冲突,同时要考虑充分利用会议室资源,请你计算至少需要多少间会议室,才能满足这些会议安排。
解题方法
排序+堆
这个做法参考了官方解答,先对所有的区间按照开始时间进行排序,使用小根堆保存每个会议的结束时间,堆的大小表示了现在已经占用了多少个会议室。
思路是,我们要申请会议室的时候,先看是不是可以释放会议室,将已经占用的会议室释放。
遍历判断每个区间的开始时间是否大于等于小根堆中最小元素(最早结束时间),如果大于等于,说明堆里面有个会议要结束了,将其弹出,代表释放了一个会议室;否则,说明堆里面的已经在使用的会议室没有空闲,只能新增加一个会议室。
由于优先占用释放的会议室,所以最后堆里面的元素个数表示总的需要占用多少个会议室。
C++代码如下:
class Solution {
public:
int minMeetingRooms(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end(), [](vector<int>& a, vector<int>&b) {return a[0] < b[0];});
priority_queue<int, vector<int>, greater<int>> que;
for (vector<int>& interval : intervals) {
if (!que.empty() && interval[0] >= que.top()) {
que.pop();
}
que.push(interval[1]);
}
return que.size();
}
};
参考资料:https://leetcode-cn.com/problems/meeting-rooms-ii/solution/hui-yi-shi-ii-by-leetcode/
日期
2019 年 9 月 17 日 —— 听了hulu宣讲会,觉得hulu的压力不大
【LeetCode】253. Meeting Rooms II 解题报告(C++)的更多相关文章
- [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 会议室之二
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] ...
- [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】47. Permutations II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- 【LeetCode】90. Subsets II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...
- 253. Meeting Rooms II
题目: 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],.. ...
- [LC] 253. Meeting Rooms II
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
随机推荐
- fluidity install on Cluster
fluidity install on Cluster 1.编译器安装 gcc4.6 openmpi python HOME_PATH= /hpcdata1/xiaozhong/ 1.1.gmp &a ...
- GWAS初探
原理 GWAS 的主要方法学依据是归纳法中的共变法,是探究复杂因果关系最主要的科学思维和方法.所谓共变法,是通过考察被研究现象发生变化的若干场合中,确定是否只有一个情况发生相应变化,如果是,那么这个发 ...
- Can't connect to HTTPS URL because the SSL module is not available. - skipping
今天用pip3安装第三方库的时候报了这样一个错: Can't connect to HTTPS URL because the SSL module is not available. - skipp ...
- EXCEL ctrl+e 百变用法不只是你用的那么简单
Excel2013版本中,新增加了一个快捷键:Ctrl+E,可以依据字符之间的关系,实现快速填充功能.一些需要使用公式或者其他功能进行解决的问题,现在只要一个快捷键就可以实现了. 用法1:快速拆解出需 ...
- 漏洞检测方法如何选?详解源代码与二进制SCA检测原理
摘要:本文探讨的是SCA具体的检测原理,源代码SCA检测和二进制SCA检测有哪些相同点和不同点,在进行安全审计.漏洞检测上各自又有什么样的优势和适用场景. 本文分享自华为云社区<源代码与二进制文 ...
- Lottie 使用
原文:https://mp.weixin.qq.com/s?__biz=MzIxNjc0ODExMA==&mid=2247485033&idx=1&sn=54dd477b4c4 ...
- android studio 报 Error:(79) Error parsing XML: not well-formed (invalid token)
android studio 报 Error:(79) Error parsing XML: not well-formed (invalid token) 我的原因是因为string 里面有< ...
- An internal error occurred during: “Updating Maven Project”. Unsupported IClasspathEntry kind=4解决办法
An internal error occurred during: "Updating Maven Project". Unsupported IClasspathEntry k ...
- docker之镜像制作
#:下载镜像并初始化系统 root@ubuntu:~# docker pull centos #:创建目录 root@ubuntu:/opt# mkdir dockerfile/{web/{nginx ...
- mybatis中返回自动生成的id
当有时我们插入一条数据时,由于id很可能是自动生成的,如果我们想要返回这条刚插入的id怎么办呢. 在mysql数据中我们可以在insert下添加一个selectKey用以指定返回的类型和值: ...