zoukankan      html  css  js  c++  java
  • Leetcode: Meeting Rooms II

    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.

    Like the previous one Meeting Room, still need to sort the intervals using a comparator.

    We need to simulate the process to know the maximum number of conference rooms needed.

    We need to maintain a minimal heap, which sort the intervals according to their finishing time.

    We add the interval to this heap one by one since they have already been sorted according to their start time.

    So this heap acctually simulates the conference rooms, the number of elements it has instataneously is the number of rooms required.

    Every time we add one interval to this heap, we need to check if its start time is greater than or equal to heap.peek(). If it is, that means there's some conferences in the heap which end now, and their rooms should be released.

    Best approach: When interval is an array: Average time: O(nlogn)

    Syntax: PriorityQueue<int[]> pq = new PriorityQueue<>((i1, i2) -> (Integer.compare(i1[1], i2[1])));

     1 class Solution {
     2     public int minMeetingRooms(int[][] intervals) {
     3         if (intervals.length < 1) return 0;
     4         Arrays.sort(intervals, (i1, i2) -> (Integer.compare(i1[0], i2[0])));
     5         
     6         PriorityQueue<int[]> pq = new PriorityQueue<>((i1, i2) -> (Integer.compare(i1[1], i2[1])));
     7     
     8         int res = 0;
     9         for (int i = 0; i < intervals.length; i ++) {
    10             while (!pq.isEmpty() && pq.peek()[1] <= intervals[i][0]) {
    11                 pq.poll();
    12             }
    13             
    14             pq.offer(intervals[i]);
    15             res = Math.max(res, pq.size());
    16         }
    17         return res;
    18     }
    19 }

    When interval is an object:

     1 /**
     2  * Definition for an interval.
     3  * public class Interval {
     4  *     int start;
     5  *     int end;
     6  *     Interval() { start = 0; end = 0; }
     7  *     Interval(int s, int e) { start = s; end = e; }
     8  * }
     9  */
    10 public class Solution {
    11     public int minMeetingRooms(Interval[] intervals) {
    12         if (intervals==null || intervals.length==0) return 0;
    13         Comparator<Interval> comp = new Comparator<Interval>() {
    14             public int compare(Interval i1, Interval i2) {
    15                 return (i1.start==i2.start)? i1.end-i2.end : i1.start-i2.start;
    16             }
    17         };
    18         
    19         Arrays.sort(intervals, comp);
    20         PriorityQueue<Interval> heap = new PriorityQueue<Interval>(intervals.length, new Comparator<Interval>() {
    21             public int compare(Interval i1, Interval i2) {
    22                 return (i1.end==i2.end)? i1.start-i2.start : i1.end-i2.end;
    23             }
    24         });
    25         
    26         int maxNum = 0;
    27         for (int i=0; i<intervals.length; i++) {
    28             if (heap.size() == 0) {
    29                 heap.offer(intervals[i]);
    30                 maxNum = Math.max(maxNum, heap.size());
    31             }
    32             else if (intervals[i].start >= heap.peek().end) {
    33                 heap.poll();
    34                 i--;
    35             }
    36             else {
    37                 heap.offer(intervals[i]);
    38                 maxNum = Math.max(maxNum, heap.size());
    39             }
    40         }
    41         return maxNum;
    42     }
    43 }

     这道题有follow up问求最多interval的时间点,返回任意一个就行。

    随便返回一个比如heap.size()最大的时候,heap.peek().end

    38行更新为

    if(heap.size() > maxNum)

      maxNum = heap.size();

      maxMoment = heap.peak.end;

    Follow up 2: 给meetings安排rooms使得optimize。可以maintain一个empty room list,每次开完会就回收,需要room就从里面random

  • 相关阅读:
    谢惠民,恽自求,易法槐,钱定边编数学分析习题课讲义16.2.3练习题参考解答[来自陶哲轩小弟]
    高考题(可作为试讲资料)
    美国数学教父拒绝10亿美元 免费分享教学视频
    梁启超死亡真相:主刀医生错把健康的肾切除
    [家里蹲大学数学杂志]第418期南开大学2013年实变函数期末考试试题参考解答
    陆启铿同志去世
    [电视剧]养父的花样年华
    [电视剧]错爱一生
    [家里蹲大学数学杂志]第413期插值不等式
    Oracle的 EXEC SQL CONTEXT学习
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/5065569.html
Copyright © 2011-2022 走看看