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

  • 相关阅读:
    linux基础学习-13.1-定时任务的介绍及分类
    linux基础学习-12.7-特殊权限-suid-粘滞位
    linux基础学习-12.6-linux系统默认权限控制命令umask
    linux基础学习-12.5-网站权限-通过控制权限让网站安全
    linux基础学习-12.4-对于目录来说r w x 是什么含义?
    linux基础学习-12.3-对于文件来说r w x 是什么含义?
    JavaWeb中文乱码解决方式
    c3p0-config.xml文件(连接数据库时可用)(重要)
    Jquery---定时器(实现页面内定时弹出广告,定时退出)
    Jquery----实现抽奖效果(根据姓名抽奖)
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/5065569.html
Copyright © 2011-2022 走看看