[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 ...
随机推荐
- modelsim命令行仿真提示“vsim 不是内部或外部命令,也不是可运行的程序或批处理文件”的解决办法
安装完modelsim后,用过命令行模式仿真,如“vsim -c -do run.do”,开始时是可以的. 后来偶然再用该仿真方式,发现命令行提示“vsim 不是内部或外部命令,也不是可运行的程序或批 ...
- String类比较,String类运算比较,String运算
String类比较,String类运算比较 >>>>>>>>>>>>>>>>>>>&g ...
- ueditor 添加微软雅黑字体 异常“从客户端中检测到有潜在危险的 request.form值”,解决
使用ueditor往数据库添加文本内容时,如果字体有css样式, <,>," 这些字符会导致报出异常信息:从客户端中检测到有潜在危险的 request.form值 因为这些字符有 ...
- Python实战:美女图片下载器,海量图片任你下载
Python应用现在如火如荼,应用范围很广.因其效率高开发迅速的优势,快速进入编程语言排行榜前几名.本系列文章致力于可以全面系统的介绍Python语言开发知识和相关知识总结.希望大家能够快速入门并学习 ...
- asp.net页面刷新等问题
windows.open 关闭当前页面刷新父页面实现() { 在子页面中 Page.ClientScript.RegisterStartupScript(this.GetType(), "a ...
- Mysql内存表的用处
文章出自:http://blog.csdn.net/hitzhang/article/details/5994639 个人最欣赏mysql的地方就是他存储引擎的多样性和可扩展性,这样mysql也能拥有 ...
- 防御SQL注入的方法总结
这篇文章主要讲解了防御SQL注入的方法,介绍了什么是注入,注入的原因是什么,以及如何防御,需要的朋友可以参考下 SQL 注入是一类危害极大的攻击形式.虽然危害很大,但是防御却远远没有XSS那么困难 ...
- 自己做的demo---宣告可以在java世界开始自由了
package $interface; public interface ILeaveHome { public abstract int a(); public abstract int b(); ...
- Java 之文件IO编程 之写入
package com.sun; /* * 操作对文件IO的写 * 2014-08-10 */ import java.io.*; public class File_Write { public s ...
- Gulp那些好用的插件 2016.04.20
开始接触LESS.组件化编程后,慢慢意识到需要一个提高工作效率的构建工具,就此接触到了Gulp. Gulp的好处在这里就不细说啦,只有四个API接口学起来简直爽歪歪,减少了大量的I/O操作,用起来很畅 ...