253. 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.

链接: http://leetcode.com/problems/meeting-rooms-ii/

题解:

给定一个interval数组,求最少需要多少间教室。初始想法是扫描线算法sweeping-line algorithm,先把数组排序,之后维护一个min-oriented heap。遍历排序后的数组,每次把interval[i].end加入到heap中,然后比较interval.start与pq.peek(),假如interval[i].start >= pq.peek(),说明pq.peek()所代表的这个meeting已经结束,我们可以从heap中把这个meeting的end time移除,继续比较下一个pq.peek()。比较完毕之后我们尝试更新maxOverlappingMeetings。 像扫描线算法和heap还需要好好复习, 直线,矩阵的相交也可以用扫描线算法。

Time Complexity - O(nlogn), Space Complexity - O(n)

/**
 * 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) {
        if(intervals == null || intervals.length == 0)
            return 0;
        
        Arrays.sort(intervals, new Comparator<Interval>() {
            public int compare(Interval t1, Interval t2) {
                if(t1.start != t2.start)
                    return t1.start - t2.start;
                else
                    return t1.end - t2.end;
            }
        });
        
        int maxOverlappingMeetings = 0;
        PriorityQueue<Integer> pq = new PriorityQueue<>();      // min oriented priority queue
        
        for(int i = 0; i < intervals.length; i++) {         // sweeping-line algorithms
            pq.add(intervals[i].end);
            while(pq.size() > 0 && intervals[i].start >= pq.peek())
                pq.remove();
                
            maxOverlappingMeetings = Math.max(maxOverlappingMeetings, pq.size());
        }
        
        return maxOverlappingMeetings;
    }
}

 

二刷:

二刷参考了@pinkfloyda的写法。对start以及end排序,然后再两个数组中对end和start进行比较。代码很简洁,速度也很快,非常值得学习。

Java:

Time Complexity - O(nlogn), Space Complexity - O(n)

/**
 * 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) {
        if (intervals == null || intervals.length == 0) return 0;
        int len = intervals.length;
        int[] starts = new int[len];
        int[] ends = new int[len];
        for (int i = 0; i < len; i++) {
            starts[i] = intervals[i].start;
            ends[i] = intervals[i].end;
        }
        Arrays.sort(starts);
        Arrays.sort(ends);
        
        int minRooms = 0, endIdx = 0;
        for (int i = 0; i < len; i++) {
            if (starts[i] < ends[endIdx]) minRooms++;
            else endIdx++;
        }
        
        return minRooms;               
    }
}

 

三刷:

class Solution {
    public int minMeetingRooms(int[][] intervals) {
        if (intervals == null || intervals.length == 0) return 0;
        Arrays.sort(intervals, (int[] i1, int[] i2) ->
                   i1[0] != i2[0] ? i1[0] - i2[0] : i1[1] - i2[1]);
        Queue<Integer> q = new PriorityQueue<>();
        int result = 0;
        for (int[] interval : intervals) {
            while (!q.isEmpty() && q.peek() <= interval[0]) q.poll();
            q.offer(interval[1]);
            result = Math.max(result, q.size());
        }
        return result;
    }
}

 

Reference:

https://leetcode.com/discuss/71846/super-easy-java-solution-beats-98-8%25

https://leetcode.com/discuss/50911/ac-java-solution-using-min-heap

https://leetcode.com/discuss/82292/explanation-super-easy-java-solution-beats-from-%40pinkfloyda

https://leetcode.com/discuss/70998/java-ac-solution-greedy-beats-92-03%25

posted @ 2015-12-02 11:51  YRB  阅读(3225)  评论(0编辑  收藏  举报