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. (转)Spring读书笔记-----Spring的Bean之Bean的基本概念

    从前面我们知道Spring其实就是一个大型的工厂,而Spring容器中的Bean就是该工厂的产品.对于Spring容器能够生产那些产品,则取决于配置文件中配置. 对于我们而言,我们使用Spring框架 ...

  2. WEB 中的一些名词解释

    OOP: 面向对象编程(Object Oriented Programming,OOP,面向对象程序设计)是一种计算机编程架构. AOP: AOP为Aspect Oriented Programmin ...

  3. DNS(域名系统)域名解析设置

    DNS(Domain Name System,域名系统), 因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP数串.通过主机名,最 ...

  4. 写一个最简单的 Server

    import java.net.*;import java.io.*;public class Server{ public static void main(String[] args) throw ...

  5. C# Ref 与out 的区别

    在C#中,有四种传递参数方式: 1. 传值 (value) : 无额外修饰符 2. 传址(reference) : 需修饰符Ref,传入函数的参数必须先赋值 3. 输出参数(output): 需修饰符 ...

  6. SQL Server备份还原数据库中的小把戏

    备份数据库时出现一个不太了解的错误 ,错误信息“is formatted to support  1 media families, but 2 media families are expected ...

  7. UIimageView GIF动画

    1.代码如下 (注释都有) - (void)viewDidLoad { [super viewDidLoad]; UIImageView * bigImageView = [[UIImageView ...

  8. 初尝 MVC4

    文章内容参考 http://www.cnblogs.com/leoo2sk/archive/2008/10/27/1320285.html 开发环境 VS2010 ,VS2010 开发 MVC4 需下 ...

  9. java 反射取得方法入参类型的泛形

    package TestReflectClass; import java.util.List; /** * Created by wangyang on 2016/12/16. */ public ...

  10. hdoj (1162) 最小生成树

    Problem B Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...