【LeetCode】253.Meeting Rooms II(Medium)解题报告

【LeetCode】253.Meeting Rooms II(Medium)解题报告

题目地址: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.

  非常重要,面试经常出现。

Solution1:

time:O(nlogn)
space:O(n)
public int minMeetingRooms(Intervals[] intervals){
    int[] starts = new int[intervals.length];
    int[] ends = new int[intervals.length];
    for(int i=0 ; iend;
    }
    Arrays.sort(starts);
    Arrays.sort(ends);
    int res = 0;
    int end = 0;
    for(int i=0 ; iif(starts[i]end]){
            res++;
        }else end++;
    }
    return res;
}

Solution2:

time:O(nlogn)
space:O(n)
public int minMeetingRooms(Intervals[] intervals){
    Arrays.sort(intervals,(a,b)->a.start-b.start);
    PriorityQuene<Interval> heap = new PriorityQuene<>(intervals.length,(a,b)->a.end-b.end);
    heap.offer(intervals[0]);
    for(int i=1 ; i{
        Interval interval = heap.poll();
        if(intervals[i].start >= interval.end){
            interval.end = intervals[i].end;
        }else{
            heap.offer(intervals[i]);
        }
        heap.offer(interval);
    }
    return heap.size();
}

Date:2018年2月19日

你可能感兴趣的:(LeetCode,Array)