题目:

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. python:执行一个命令行N次

    经常希望可以执行一个命令行N次...windows下没有现成的工具(有?推荐给我!) 用python写一个... #!/usr/bin/evn python #coding: utf-8 " ...

  2. 8、WPF体系架构和运行机制

    体系架构:http://msdn.microsoft.com/zh-cn/library/ms750441.aspx 运行机制:http://www.cnblogs.com/leep2007/arch ...

  3. Legacy安装win7和Ubuntu14.04双系统

    Legacy安装win7和Ubuntu14.04双系统 安装环境 Legacy启动模式(传统引导) 笔记本已安装win7 硬盘启动顺序为: U盘 硬盘 光驱 安装方法 制作U盘启动盘 在Ubuntu官 ...

  4. Export Farm Solution wsp Files SharePoint 2007 and 2010

    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")$farm = [Microsof ...

  5. (转)使用 /proc 文件系统来访问 Linux 内核的内容

    转载网址:http://www.ibm.com/developerworks/cn/linux/l-proc.html 这个虚拟文件系统在内核空间和用户空间之间打开了一个通信窗口/proc 文件系统是 ...

  6. Careercup - Facebook面试题 - 23594662

    2014-05-02 03:19 题目链接 原题: Given a sequence of numbers A() ..A(n), find the continuous subsequenceA(i ...

  7. textview点击后selector的pressed无效果

    原因: 需要配置  android:clickable="true" 这个跟开发环境有关,我之前用的android studio 就不需要这一项,默认可以点击. ********* ...

  8. ffmpeg 音频转码

    大多数厂家摄像机输出的音频流格式都是PCM,有一些场合(比如讲音视频流保存成Ts流)需要将PCM格式转成AAC格式.基本的思路是先解码得到音频帧,再将音频帧编码成AAC格式.编码和解码之间需要添加一个 ...

  9. html+css学习笔记 4[定位]

    如何让图1中的div2移动到如图2上的位置: 思路:哪些css命令能够影响盒子显示的位置呢? relative相对定位/定位偏移量 position:relative;  相对定位         a ...

  10. linux使用:vi编辑器

    初学linux,目前是概念多于操作,所以记录下一些操作: 编辑某个文件():vi 文件名 编辑后保存退出::wq 编辑后不保存退出: :q! 参数:-R 只读模式 -x 文件加密(vim命令下使用) ...