[LeetCode] 253. Meeting Rooms II 会议室 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.
For example,
Given [[0, 30],[5, 10],[15, 20]],
return 2.
252. Meeting Rooms 的拓展,同样给一个开会的区间数组,返回最少需要的房间数。
解法1: 把区间变成2个数组:start时间数组和end时间数组,并对两个数组排序。然后一个指针遍历start数组,另一个指针指向end数组。如果start时间小于end时间,房间数就加1,start时间加1,比较并记录出现过的最多房间数。start时间大于end,则所需房间数就减1,end指针加1。
解法2:最小堆minHeap,先按start排序,然后维护一个minHeap,堆顶元素是会议结束时间最早的区间,也就是end最小。每次比较top元素的end时间和当前元素的start时间,如果end < start,说明该room可以结束接下来被当前会议区间使用。最后返回堆的大小就是所需的房间数。
面试follow up: 结果要将会议名称跟对应房间号一起返回,而不仅仅是算需要的房间数目。
Java:
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public int minMeetingRooms(Interval[] intervals) {
if(intervals == null || intervals.length == 0) return 0;
int min = 0; int max = 0;
for(int i=0; i<intervals.length; i++){
min = Math.min(min, intervals[i].start);
max = Math.max(max, intervals[i].end);
} int[] count = new int[max-min+1];
for(int i=0; i<intervals.length; i++){
count[intervals[i].start]++;
count[intervals[i].end]--;
}
int maxroom = Integer.MIN_VALUE;
int num = 0;
for(int i=0; i<count.length; i++){
num += count[i];
maxroom = Math.max(maxroom, num);
}
return maxroom;
}
}
Java:minHeap
public class Solution {
public int minMeetingRooms(Interval[] intervals) {
int n=intervals.length;
Arrays.sort(intervals, new Comparator<Interval>(){
public int compare(Interval a, Interval b) {
return a.start-b.start;
}
});
PriorityQueue<Integer> pq=new PriorityQueue<>();
for (int i=0; i<n; i++) {
if (i>0 && intervals[i].start>=pq.peek()) pq.poll();
pq.add(intervals[i].end);
}
return pq.size();
}
}
Python:
class Solution:
# @param {Interval[]} intervals
# @return {integer}
def minMeetingRooms(self, intervals):
starts, ends = [], []
for i in intervals:
starts.append(i.start)
ends.append(i.end) starts.sort()
ends.sort() s, e = 0, 0
min_rooms, cnt_rooms = 0, 0
while s < len(starts):
if starts[s] < ends[e]:
cnt_rooms += 1 # Acquire a room.
# Update the min number of rooms.
min_rooms = max(min_rooms, cnt_rooms)
s += 1
else:
cnt_rooms -= 1 # Release a room.
e += 1 return min_rooms
C++:
class Solution {
public:
int minMeetingRooms(vector<Interval>& intervals) {
vector<int> starts, ends;
for (const auto& i : intervals) {
starts.emplace_back(i.start);
ends.emplace_back(i.end);
}
sort(starts.begin(), starts.end());
sort(ends.begin(), ends.end());
int min_rooms = 0, cnt_rooms = 0;
int s = 0, e = 0;
while (s < starts.size()) {
if (starts[s] < ends[e]) {
++cnt_rooms; // Acquire a room.
// Update the min number of rooms.
min_rooms = max(min_rooms, cnt_rooms);
++s;
} else {
--cnt_rooms; // Release a room.
++e;
}
}
return min_rooms;
}
};
C++: minHeap
class Solution {
public:
int minMeetingRooms(vector<Interval>& intervals) {
sort(intervals.begin(), intervals.end(), [](const Interval &a, const Interval &b){return a.start < b.start;});
priority_queue<int, vector<int>, greater<int>> q;
for (auto a : intervals) {
if (!q.empty() && q.top() <= a.start) q.pop();
q.push(a.end);
}
return q.size();
}
};
类似题目:
[LeetCode] 252. Meeting Rooms 会议室
[LeetCode] 56. Merge Intervals 合并区间
All LeetCode Questions List 题目汇总
[LeetCode] 253. Meeting Rooms II 会议室 II的更多相关文章
- [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 会议室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 252. Meeting Rooms (会议室)$
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- 252. Meeting Rooms 区间会议室
[抄题]: 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 ...
- 【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],.. ...
- 253. Meeting Rooms II
题目: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] ...
随机推荐
- SQL进阶系列之4HAVING字句的力量
写在前面 SQL是面向集合的语言,与面向过程和面向对象语言都不一样 寻找缺失的编号 /* 寻找缺失的编号 */ CREATE TABLE SeqTbl (seq INTEGER PRIMARY KEY ...
- PAT甲级1011水题飘过
题目分析:对于输入的数据分三条,选出每条中最大值记录下来,按照题目要求算出最大可能的获利即可 #include<iostream> using namespace std; ]; //k数 ...
- 使用eclipse-hadoop插件无法再eclipse操作(上传、删除文件)
再conf中的hdfs-site.xml添加如下配置: <property><name>dfs.permissions</name><value>fal ...
- 关于AndroidStudio的apk打包遇到的问题记录
12月份末尾,想来个总结,主要是得记一些重要的. 首先就得是AndroidStudi内的apk打包,就是弄当前项目app的安装包出来. 下面就说下具体步骤和注意问题. 首先 : 看到AndroidSt ...
- hdu6172&&hdu6185&&P5487——BM算法
hdu6172 模板的简单应用 先根据题中的表达式求出前几项,再上BM,注意一下n的大小关系. #include <bits/stdc++.h> using namespace std; ...
- 10.29-10.30Test
10.29-10.30Test 查看请点个赞 转载请注明出处(~不然~) 题目 描述 做法 \(BSOJ5161\) 从小到大放入\(n\)个数,每次他可以覆盖没被覆盖且小于等于自己的数,求每个数覆盖 ...
- JS 不声明第三个变量的情况下实现两数变换
1. var a = 1; var b = 2; a = a + b; b = a - b; a = a - b; console.log(a); console.log(b); 2. var a = ...
- Function函数的声明方式
函数 函数是一段可以反复利用的代码 Function函数的声明方式, +通过变量,把函数存储到变量容器里 var a=function(){ console.log("大瓜皮") ...
- 使用terraform 生成自签名证书
terraform 是一个很不错的基础设施工具,我们可以用来做关于基础设施部署的事情,可以实现基础设施即代码 以下演示一个简单的自签名证书的生成(使用tls provider) main.tf 文件 ...
- 洛谷 题解 UVA572 【油田 Oil Deposits】
这是我在洛谷上的第一篇题解!!!!!!!! 这个其实很简单的 我是一只卡在了结束条件这里所以一直听取WA声一片,详细解释代码里见 #include<iostream> #include&l ...