[LeetCode#253] Meeting Rooms II
Problem:
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
.
Analysis:
This problem likes skyline problem very much, although we could the say the problem is much easy.
The idea is to use a kind of greedy problem.
Basic idea:
We use a priority queue to record each room's end time, the earliest available room's end time is at the the minimum heap's top.
Now we have a new interval (meeting).
1. If the meeting's start time after the earilest room's end time. It means we could actually arrange the new meeting into this room, we have no need to open a new room.
---------------------------------------------------------------
if (intervals[i].start >= min_heap.peek()) {
min_heap.poll();
min_heap.offer(intervals[i].end);
} The reason why we must append the new meeting after the earilist avaialbe room, what if there are also other rooms available at that time?
Suppose we have two meeting room, and a new meeting. And A is the earilist available room.
A [ ] new_1[ ]
B [ ] new_1[ ]
Wheather we add the new meeting into room A or room B, it actually would result in the same new end time for that room. And we know all other meetings must happen after the new meeting. Suppose we have a new meeting called "new_2". iff new_1 was added into room A
A [ ] new_1[ ]
B [ ] new_2[ ] iff new_2 was added into room B
A [ ] new_2[ ]
B [ ] new_1[ ] As you can see from the change!!! If we wipe out the name of each room, it actually result in same available time structure among rooms. 2. If the meeting's start time before the earilest room's end time. It means this meeting actually conflict with all other room's meeting, we have to open a new room.
---------------------------------------------------------------
if (intervals[i].start < min_heap.peek()) {
min_heap.offer(intervals[i].end);
count++;
}
---------------------------------------------------------------
Solution:
public class Solution {
public int minMeetingRooms(Interval[] intervals) {
if (intervals == null)
throw new IllegalArgumentException("intervals is null");
int len = intervals.length;
if (len <= 1)
return len;
Arrays.sort(intervals, new Comparator<Interval>() {
@Override
public int compare(Interval a, Interval b) {
if (a.start == b.start)
return a.end - b.end;
return a.start - b.start;
}
});
PriorityQueue<Integer> min_heap = new PriorityQueue<Integer> (10);
min_heap.offer(intervals[0].end);
int count = 1;
for (int i = 1; i < len; i++) {
if (intervals[i].start < min_heap.peek()) {
min_heap.offer(intervals[i].end);
count++;
} else{
min_heap.poll();
min_heap.offer(intervals[i].end);
}
}
return count;
}
}
[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 会议室之二
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 解题报告(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],.. ...
- [LC] 253. Meeting Rooms II
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- [LeetCode] 252. Meeting Rooms 会议室
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 ...
随机推荐
- Windows Server 2008 R2 域控制器部署指南
一.域控制器安装步骤: 1.装 Windows Server 2008 R2并配置计算机名称和IP地址(见 附录一) 2.点击“开始”,在“搜索程序和文件”中输入Dcpromo.exe后按回车键: 3 ...
- Java根据ip地址获取Mac地址,Java获取Mac地址
Java根据ip地址获取Mac地址,Java获取Mac地址 >>>>>>>>>>>>>>>>>&g ...
- Caused by: java.lang.ClassNotFoundException: com/sun/tools/internal/xjc/api/XJC
Caused by: java.lang.ClassNotFoundException: com/sun/tools/internal/xjc/api/XJC 缺少com/sun/tools/inte ...
- volatile用处说明
在JDK1.2之前,Java的内存模型实现总是从主存(即共享内存)读取变量,是不需要进行特别的注意的.而随着JVM的成熟和优化,现在在多线程环境下volatile关键字的使用变得非常重要. 在当前 ...
- Java-Android 之页面的跳转和结构的搭建
Android中每个页面就是一个Activity,要合理的让这些页面实现跳转,才是关键,这里讲一个最简单的 首先,有一个主页面main.xml <?xml version="1.0&q ...
- [时间操作] C#DateFormat时间帮助类 (转载)
点击下载 DateFormat.rar 主要功能如下 返回每月的第一天和最后一天 看下面代码吧 /// <summary> /// 类说明:时间操作类 /// 编 码 人:苏飞 /// 联 ...
- IXListView的自我分析一
XListView是一个很不错的用来刷新和加载的控件,下拉刷新和上拉加载.目前这个控件已经没有更新,这个不重要,重要的是它确实还不错,之后可能一直有人在用. android没有提供原生的这类控件,需要 ...
- 使用with语句来写一个稍微复杂sql语句,附加和子查询的性能对比
今天偶尔看到sql中也有with关键字,好歹也写了几年的sql语句,居然第一次接触,无知啊.看了一位博主的文章,自己添加了一些内容,做了简单的总结,这个语句还是第一次见到,学习了.我从简单到复杂地写, ...
- c#equals相关
1.==是直接比较值类型的值或引用类型的引用地址,但==不能用于struct,struct只能用equals来比较.==一般情况下与object.equals得到的结果是相等的. 2.Referenc ...
- mysql 中执行的 sql 注意字段之间的反向引号和单引号
如下的数据表 create table `test`( `id` int(11) not null auto_increment primary key, `user` varchar(100) no ...