LeetCode 253. Meeting Rooms II(会议室)

原题网址: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() {
            @Override
            public int compare(Interval i1, Interval i2) {
                return Integer.compare(i1.start, i2.start);
            }
        });
        PriorityQueue minHeap = new PriorityQueue<>();
        int rooms = 0;
        for(int i=0; i

更直观的实现方法:

/**
 * 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() {
            @Override
            public int compare(Interval i1, Interval i2) {
                return Integer.compare(i1.start, i2.start);
            }
        });
        int rooms = 0;
        int active = 0;
        PriorityQueue heap = new PriorityQueue<>();
        for(int i=0; i


方法二:分别对起始时间和结束时间排序,由于会议之间并无差异(不像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

参考文章:

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

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


你可能感兴趣的:(冲突,重叠,移动窗口,窗口,括号匹配,段落,持续,起止,会议室,困难,区间,连续,时间序列)