码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode#253] Meeting Rooms II

时间:2015-09-13 13:18:35      阅读:634      评论:0      收藏:0      [点我收藏+]

标签:

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#253] Meeting Rooms II

标签:

原文地址:http://www.cnblogs.com/airwindow/p/4804537.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!