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;
        }
    }
  • 相关阅读:
    sql server 字符串转成日期格式
    删除重复的数据
    sql server 去掉前后空格
    取出分组后每组的第一条记录(不用group by)按时间排序
    选中datagridview要选择的行
    sql server 跨表修改
    winfrom 打印表单
    .net导入Excel数据遇到问题(SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT 'OpenRowset/OpenDataso) .
    ComboBox绑定数据源后再添加一条记录
    机器学习 | 一个基于机器学习的简单小实践:波斯顿房价预测分析
  • 原文地址:https://www.cnblogs.com/airwindow/p/4804537.html
Copyright © 2011-2022 走看看