LeetCode 253. Meeting Rooms II(會議室)

jmspan發表於2016-04-08

原題網址:https://leetcode.com/problems/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.

方法一:對起始時間進行排序,使用最小堆來記錄當前會議的結束時間,當心會議的起始時間大於最小堆中的最早結束時間,說明新會議與堆中的最早結束會議不重疊。

/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
public class Solution {
    public int minMeetingRooms(Interval[] intervals) {
        Arrays.sort(intervals, new Comparator<Interval>() {
            @Override
            public int compare(Interval i1, Interval i2) {
                return Integer.compare(i1.start, i2.start);
            }
        });
        PriorityQueue<Integer> minHeap = new PriorityQueue<>();
        int rooms = 0;
        for(int i=0; i<intervals.length; i++) {
            minHeap.offer(intervals[i].end);
            if (intervals[i].start < minHeap.peek()) {
                rooms ++;
            } else {
                minHeap.poll();
            }
        }
        return rooms;
    }
}

更直觀的實現方法:

/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
public class Solution {
    public int minMeetingRooms(Interval[] intervals) {
        Arrays.sort(intervals, new Comparator<Interval>() {
            @Override
            public int compare(Interval i1, Interval i2) {
                return Integer.compare(i1.start, i2.start);
            }
        });
        int rooms = 0;
        int active = 0;
        PriorityQueue<Integer> heap = new PriorityQueue<>();
        for(int i=0; i<intervals.length; i++) {
            while (!heap.isEmpty() && heap.peek() <= intervals[i].start) {
                active --;
                heap.poll();
            }
            active ++;
            heap.offer(intervals[i].end);
            rooms = Math.max(rooms, active);
        }
        return rooms;
    }
}


方法二:分別對起始時間和結束時間排序,由於會議之間並無差異(不像skyline問題,不同建築的高度不一樣),所以分別使用兩個指標來推進起始時間和結束時間。

/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
public class Solution {
    public int minMeetingRooms(Interval[] intervals) {
        int[] starts = new int[intervals.length];
        int[] ends = new int[intervals.length];
        for(int i=0; i<intervals.length; i++) {
            starts[i] = intervals[i].start;
            ends[i] = intervals[i].end;
        }
        Arrays.sort(starts);
        Arrays.sort(ends);
        int rooms = 0;
        int activeMeetings = 0;
        int i=0, j=0;
        while (i < intervals.length && j < intervals.length) {
            if (starts[i] < ends[j]) {
                activeMeetings ++;
                i ++;
            } else {
                activeMeetings --;
                j ++;
            }
            rooms = Math.max(rooms, activeMeetings);
        }
        return rooms;
    }
}

參考文章:

http://buttercola.blogspot.com/2015/08/leetcode-meeting-rooms-ii.html

http://www.jyuan92.com/blog/leetcode-meeting-rooms-ii/


相關文章