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

  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 会议室之二

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

  3. [leetcode]253. Meeting Rooms II 会议室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 解题报告(C++)

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

  5. 253. Meeting Rooms II

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

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

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

  7. [LC] 253. Meeting Rooms II

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

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

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

  9. [LeetCode] Meeting Rooms II 会议室之二

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

随机推荐

  1. 【bzoj1212】 [HNOI2004]L语言

    题目描述 标点符号的出现晚于文字的出现,所以以前的语言都是没有标点的.现在你要处理的就是一段没有标点的文章. 一段文章T是由若干小写字母构成.一个单词W也是由若干小写字母构成.一个字典D是若干个单词的 ...

  2. HTML 转义字符

    在HTML中,一个包含特殊字符(如<>&)的字符串,要显示在页面上,由于添加到文本节点时会被认为是HTML的标签结构,造成一些错误,因此,要将这些特殊字符进行转义. 例如在< ...

  3. Jquery Datatables(三)

    最近在项目中又使用Datatables的一个有趣功能,官网列子如下图: 点击“+”,展开列表,再次点击收缩. 官网的列子点击展开后的数据也是原来行中的数据,这边有了一个想法是否可以利用Ajax去动态加 ...

  4. EnumWindows 使用

    转载自csdn:http://blog.csdn.net/hairi/article/details/548064   EnumWindows 用来列举屏幕上所有顶层窗口. MSDN原话: The E ...

  5. Orcale安装完成后 em管理、性能无法登陆 报:没有找到主机

    先在我的电脑环境变量中加入oracle_sid=orcl 在Orcale主目录中查找emd.properties 文件修改(时间格式) agentTZRegion=GMT agentTZRegion= ...

  6. VMWare ESX Server

    VMWare ESX Server 4.1 全套下载 [转自:http://www.awolf.net/content/hack/vmware-esx-server-4-1-all-download. ...

  7. jQuery实现多级手风琴树形下拉菜单(源码)

    前几天因为公司的菜单要调整,公司的UI框架是不支持的,所以就自己在网上找了一个下拉菜单,可以支持多级菜单数据的,菜单数据是从xml文件中配置后读取的,网上有许多这方面的例子感觉不是很好用,就打了个包贴 ...

  8. 基于url拦截实现权限控制

    用户表,角色表,用户角色表,权限表,权限角色表 1.用户通过认证(可以是验证用户名,密码等) 2.登陆拦截器,为公开的url放行, 登陆时,将用户信息放入session中,获得用户的权限集合,将集合放 ...

  9. c语言字符数组与字符串的使用详解

    转自:http://www.jb51.net/article/37456.htm 1.字符数组的定义与初始化字符数组的初始化,最容易理解的方式就是逐个字符赋给数组中各元素.char str[10]={ ...

  10. Binary Tree Level Order Traversal 解题思路 ×

    要求: 树的层级遍历 思路: 1.两个队列,q1 q2 ,root放到q1 2.q1首元素出列,判断是否有左右孩子,有的话,放入q2.(循环此步骤值得q1为空) 3.q1 = q2,重复2,直到q1为 ...