253. Meeting Rooms II

253. Meeting Rooms II
用 peek。不可以改变 interval 吗 ?
再试一下 pq 只存结束时间的

感觉第一个 naive 的 解法也要会讲, greedy 的 , 不用 pq 的 。 
Sweep line  ?? 

public int minMeetingRooms(Interval[] intervals) {
  if (intervals == null || intervals.length == 0) return 0;
  Arrays.sort(intervals, new Comparator<Interval>(){
    public int compare(Interval i1, Interval i2) {
      return i1.start - i2.start;
    }
  });
  PriorityQueue<Interval> pq = new PriorityQueue<>(new Comparator<Interval>(){
    public int compare(Interval i1, Interval i2) {
      return i1.end - i2.end;
    }
  });
  pq.offer(intervals[0]);
  for (int i = 1; i < intervals.length; i++) {
    Interval interval = pq.poll();
    if (intervals[i].start >= interval.end) 
      interval.end = intervals[i].end;
    else
      pq.offer(intervals[i]);
    pq.offer(interval);
  }
  return pq.size();
}

 

if the new interval's start is later than the smallest end index in our pq. then we can use the same room, and update the end , put it back to the pq. 

otherwise, we open a new room

Google

其中注意的是有两轮是刷题耳武伞的变形,比如求点在不在区间,最高的区间之类的.

 

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.

Example 1:

Input: [[0, 30],[5, 10],[15, 20]]
Output: 2

Example 2:

Input: [[7,10],[2,4]]
Output: 1

 

posted on 2018-08-09 17:03  猪猪&#128055;  阅读(129)  评论(0)    收藏  举报

导航