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的更多相关文章

  1. [LeetCode] 253. Meeting Rooms 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#253] Meeting Rooms II

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

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

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

  5. 252. Meeting Rooms 区间会议室

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

  6. [LeetCode] 252. Meeting Rooms 会议室

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

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

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

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

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

  9. 253. Meeting Rooms II

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

随机推荐

  1. 数据库索引数据结构总结——ART树就是前缀树

    数据库索引数据结构总结 from:https://zhewuzhou.github.io/2018/10/18/Database-Indexes/ 摘要 数据库索引是数据库中最重要的组成部分,而索引的 ...

  2. 微信之获取微信的openid(二)详细版

    第一步 :配置测试号,网页授权获取用户基本信息. 该授权回掉页面域名为ngrok 映射的域名,我的映射地址是127.0.0.1:8080. 到此微信配置完毕,接下来就是直接上代码了 2.用户同意授权 ...

  3. js访问对象属性的方式“.”与“[]”的区别

    . 和 [] 没多大区别,作用完全相同.但是 一般情况下建议使用 . 写法,这样比较接近其它语言的面向对象写法,易读 如果属性名是动态的(比如变量中),只能使用 [] 写法.如 var person= ...

  4. BBS总结 --- django设计

    目录 1.调用模块使用 2.BBS中urls.py 3.django中配置 4.新学方法使用 5.BBS用到的知识点 1.调用模块使用 from django.db import models fro ...

  5. linux 判空处理

    linux在进行判空是一个经常要用到的操作,可以使用如下方式: 变量通过" "引号引起来 if [ ! -n "$filename" ];then echo & ...

  6. js判断是否是在微信浏览器中打开

    // js判断是否是在微信浏览器中打开 function is_weixn(){ var ua = navigator.userAgent.toLowerCase(); if(ua.match(/Mi ...

  7. LOJ P10004 智力大冲浪 题解

    每日一题 day37 打卡 Analysis 经典的带限期和罚款的单位时间任务调度问题 将 val 从大到小排序,优先处理罚款多的,将任务尽量安排在期限之前,并且靠后,如果找不到,则放在最后面 #in ...

  8. 超级好用的excel导出方法,比phpexcel快n倍,并且无乱码

    public function exportToExcel($filename, $tileArray=[], $dataArray=[]){ ini_set('memory_limit','512M ...

  9. HDU 1542.Atlantis-线段树求矩形面积并(离散化、扫描线/线段树)-贴模板

    好久没写过博客了,这学期不是很有热情去写博客,写过的题也懒得写题解.现在来水一水博客,写一下若干年前的题目的题解. Atlantis Time Limit: 2000/1000 MS (Java/Ot ...

  10. GoCN每日新闻(2019-10-15)

    GoCN每日新闻(2019-10-15) GoCN每日新闻(2019-10-15) 1. Go Module 存在的意义与解决的问题 https://www.ardanlabs.com/blog/20 ...