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:

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

Example 2:

  1. Input: [[7,10],[2,4]]
  2. 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,并不用分配新的房间,而结束时间的映射值为负数更不会增加房间数,利用这种思路可以写出代码如下:

解法一:

  1. class Solution {
  2. public:
  3. int minMeetingRooms(vector<vector<int>>& intervals) {
  4. map<int, int> m;
  5. for (auto a : intervals) {
  6. ++m[a[]];
  7. --m[a[]];
  8. }
  9. int rooms = , res = ;
  10. for (auto it : m) {
  11. res = max(res, rooms += it.second);
  12. }
  13. return res;
  14. }
  15. };

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

解法二:

  1. class Solution {
  2. public:
  3. int minMeetingRooms(vector<vector<int>>& intervals) {
  4. vector<int> starts, ends;
  5. int res = , endpos = ;
  6. for (auto a : intervals) {
  7. starts.push_back(a[]);
  8. ends.push_back(a[]);
  9. }
  10. sort(starts.begin(), starts.end());
  11. sort(ends.begin(), ends.end());
  12. for (int i = ; i < intervals.size(); ++i) {
  13. if (starts[i] < ends[endpos]) ++res;
  14. else ++endpos;
  15. }
  16. return res;
  17. }
  18. };

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

解法三:

  1. class Solution {
  2. public:
  3. int minMeetingRooms(vector<vector<int>>& intervals) {
  4. sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>& b){ return a[] < b[]; });
  5. priority_queue<int, vector<int>, greater<int>> q;
  6. for (auto interval : intervals) {
  7. if (!q.empty() && q.top() <= interval[]) q.pop();
  8. q.push(interval[]);
  9. }
  10. return q.size();
  11. }
  12. };

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. let/const特性

        let: 1.声明的变量不存在预解析: console.log(a); let a=1; 2.变量名不允许重复(在同一作用域下): { let a=1; let a=2; console.lo ...

  2. jersey常用注解解释 JAX-RS常用注解:

    jersey常用注解解释: Annotation 作用 说明 @GET 查询请求 相当于数据库的查询数据操作 @PUT 更新请求 相当于数据库的更新数据操作 @POST 插入请求 相当于数据库的插入数 ...

  3. JVM指令手册

    JVM指令大全 常量入栈指令 指令码 操作码(助记符) 操作数 描述(栈指操作数栈) 0x01 aconst_null null值入栈. 0x02 iconst_m1 -1(int)值入栈. 0x03 ...

  4. PHP 数组函数大全

    PHP数组函数是核心的一部分.无需安装即可使用这些函数 函数名称 描述 array_change_key_case 将数组中的所有键名修改为全大写或小写 array_chunk 将一个数组分割成多个 ...

  5. AutoLayout的使用

    虽然苹果提供了AutoresizingMask的布局方式,这个方式局限性太大:只能解决父控件和子控件间的相对关系: 因此,推出了AutoLayout:苹果官方也是推荐开发者尽量使用autolayout ...

  6. Struts2 常量配置

    除了action的配置,struts还有其他的一些配置,比如编码方式. 这些配置用键值对来表示,键是固定的,是常量,所以也叫做常量配置. 常量配置有5种方式,Struts会按以下顺序依次搜索加载常量( ...

  7. Django 练习班级管理系统四 -- 编辑班级

    修改 classes.html {% extends "layout.html" %} {% block css %} {% endblock %} {% block conten ...

  8. 数字、字符串、列表、字典,jieba库,wordcloud词云

    一.基本数据类型 什么是数据类型 变量:描述世间万物的事物的属性状态 为了描述世间万物的状态,所以有了数据类型,对数据分类 为什么要对数据分类 针对不同的状态需要不同的数据类型标识 数据类型的分类 二 ...

  9. oracle dg状态检查及相关命令

    oracle dg 状态检查 先检查备库的归档日志同步情况 SELECT NAME,applied FROM v$archived_log; alter database recover manage ...

  10. Linux 用epoll实现的简单http服务器

    Linux 用epoll实现的简单http服务器 main.c #include <stdio.h> #include <sys/types.h> #include <s ...