zoukankan      html  css  js  c++  java
  • [LeetCode#253] Meeting Rooms II

    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;
        }
    }
  • 相关阅读:
    堡垒问题
    装载问题
    最长公共子序列(LCS)
    windows 8(8.1) 、windows 7 、linux(fadora,ubuntu) 三个系统安装方法介绍
    编译sass,遇到报错error style.scss (Line 3: Invalid GBK character "\xE5")
    使用sublime text3手动安装插件
    win7 安装 nodesass报错
    收藏的一些github开源项目,在这里记录一下
    总是有人问我,那你能造出你自己都搬不动的石头吗? 我说不能,但我能写出个我自己都无法 fix 的 bug。
    genmotion 安装 app 报错 This application is't compatible with your mobile phone解决办法
  • 原文地址:https://www.cnblogs.com/airwindow/p/4804537.html
Copyright © 2011-2022 走看看