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;
        }
    }
  • 相关阅读:
    前端与算法 leetcode 28.实现 strStr()
    前端与算法 leetcode 27.移除元素
    npm钉钉脚手架,支持考勤信息获取
    VSCode 使用Settings Sync同步配置(最新版教程,非常简单)
    VSCode 远程开发(带免密)
    vscode保存代码,自动按照eslint规范格式化代码设置
    matplotlib 3D数据-【老鱼学matplotlib】
    matplotlib等高线图-【老鱼学matplotlib】
    matplotlib柱状图-【老鱼学matplotlib】
    matplotlib散点数据-【老鱼学matplotlib】
  • 原文地址:https://www.cnblogs.com/airwindow/p/4804537.html
Copyright © 2011-2022 走看看