题目:

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.

链接: http://leetcode.com/problems/meeting-rooms-ii/

题解:

给定一个interval数组,求最少需要多少间教室。初始想法是扫描线算法sweeping-line algorithm,先把数组排序,之后维护一个min-oriented heap。遍历排序后的数组,每次把interval[i].end加入到heap中,然后比较interval.start与pq.peek(),假如interval[i].start >= pq.peek(),说明pq.peek()所代表的这个meeting已经结束,我们可以从heap中把这个meeting的end time移除,继续比较下一个pq.peek()。比较完毕之后我们尝试更新maxOverlappingMeetings。 像扫描线算法和heap还需要好好复习, 直线,矩阵的相交也可以用扫描线算法。

Time Complexity - O(nlogn), Space Complexity - O(n)

/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public int minMeetingRooms(Interval[] intervals) {
if(intervals == null || intervals.length == 0)
return 0; Arrays.sort(intervals, new Comparator<Interval>() {
public int compare(Interval t1, Interval t2) {
if(t1.start != t2.start)
return t1.start - t2.start;
else
return t1.end - t2.end;
}
}); int maxOverlappingMeetings = 0;
PriorityQueue<Integer> pq = new PriorityQueue<>(); // min oriented priority queue for(int i = 0; i < intervals.length; i++) { // sweeping-line algorithms
pq.add(intervals[i].end);
while(pq.size() > 0 && intervals[i].start >= pq.peek())
pq.remove(); maxOverlappingMeetings = Math.max(maxOverlappingMeetings, pq.size());
} return maxOverlappingMeetings;
}
}

二刷:

二刷参考了@pinkfloyda的写法。对start以及end排序,然后再两个数组中对end和start进行比较。代码很简洁,速度也很快,非常值得学习。

Java:

Time Complexity - O(nlogn), Space Complexity - O(n)

/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public int minMeetingRooms(Interval[] intervals) {
if (intervals == null || intervals.length == 0) return 0;
int len = intervals.length;
int[] starts = new int[len];
int[] ends = new int[len];
for (int i = 0; i < len; i++) {
starts[i] = intervals[i].start;
ends[i] = intervals[i].end;
}
Arrays.sort(starts);
Arrays.sort(ends); int minRooms = 0, endIdx = 0;
for (int i = 0; i < len; i++) {
if (starts[i] < ends[endIdx]) minRooms++;
else endIdx++;
} return minRooms;
}
}

三刷:

class Solution {
public int minMeetingRooms(int[][] intervals) {
if (intervals == null || intervals.length == 0) return 0;
Arrays.sort(intervals, (int[] i1, int[] i2) ->
i1[0] != i2[0] ? i1[0] - i2[0] : i1[1] - i2[1]);
Queue<Integer> q = new PriorityQueue<>();
int result = 0;
for (int[] interval : intervals) {
while (!q.isEmpty() && q.peek() <= interval[0]) q.poll();
q.offer(interval[1]);
result = Math.max(result, q.size());
}
return result;
}
}

Reference:

https://leetcode.com/discuss/71846/super-easy-java-solution-beats-98-8%25

https://leetcode.com/discuss/50911/ac-java-solution-using-min-heap

https://leetcode.com/discuss/82292/explanation-super-easy-java-solution-beats-from-%40pinkfloyda

https://leetcode.com/discuss/70998/java-ac-solution-greedy-beats-92-03%25

253. Meeting Rooms II的更多相关文章

  1. [LeetCode] 253. Meeting Rooms 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#253] Meeting Rooms II

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

  4. [leetcode]253. Meeting Rooms II 会议室II

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

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

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

  6. [LC] 253. Meeting Rooms II

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

  7. 【LeetCode】253. Meeting Rooms II 解题报告(C++)

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

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

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

  9. LeetCode Meeting Rooms II

    原题链接在这里:https://leetcode.com/problems/meeting-rooms-ii/ Given an array of meeting time intervals con ...

随机推荐

  1. Nginx+Tomcat动静分离

    需求:nginx处理用户请求的静态页面,tomcat处理用户请求jsp页面,来实现动态分离,nginx处理静态页面效率远高于tomcat,这样一来就能更好的提高并发,处理性能. 准备软件: 下载jdk ...

  2. C程序调用shell脚本共有三种方法

    C程序调用shell脚本共有三种法子 :system().popen().exec系列函数call_exec1.c ,内容为:system() 不用你自己去产生进程,它已经封装了,直接加入自己的命令e ...

  3. WPF学习05:2D绘图 使用Transform进行控件变形

    在WPF学习04:2D绘图 使用Shape绘基本图形中,我们了解了如何绘制基本的图形. 这一次,我们进一步,研究如何将图形变形. 例子 一个三角形,经Transform形成组合图形: XAML代码: ...

  4. linux终端io笔记

    简介 终端的两种工作模式:以行为单位的工作模式,以字符数或时间为单位自定义模式 终端判断函数: int isatty(int fd) 终端属性的获取与设置: int tcgetattr(int fd, ...

  5. IIS本地服务器,设置IP地址问题

    IIS启动本地网站的时候,设置了IP地址为本地IP,并绑定特定的端口号,电脑无法打开,但是同个局域网的手机可以打开.

  6. Java的别名机制

    基本类型存储了实际的数值,而并非指向一个对象的引用,所以在为其赋值的时候,是直接将一个地方的内容复制到另一个地方. 但是在为对象"赋值"的时候,情况却发生了变化.对一个对象进行操作 ...

  7. VBS基础篇 - FileSystemObject对象

    文件系统是所有操作系统最重要的部分之一,脚本经常会需要对文件及文件夹进行访问和管理,在Vbs中对桌面和文件系统进行访问的顶级对象是FileSystemObject FSO包含的常见对象有:       ...

  8. LintCode-Majority Number

    Given an array of integers, the majority number is the number that occurs more than half of the size ...

  9. 团队博客作业Week1 Team Homework #3软件工程在北航

    这次我们采访了一位大四的学姐,让她简单地谈了谈去年学习软件工程的经历和感受. 在完成软件工程大作业的过程中,由于计划安排与实际脱节,导致时间前松后紧,平均每周花在这门课上的时间大约有8个小时. 项目完 ...

  10. oracle 判断是否数字 正则表达式法

     SELECT '-100'    FROM dual   WHERE REGEXP_LIKE('-100','(^[+-]?\d{0,}\.?\d{0,}$)'); REGEXP_LIKE 用法: ...