/**
* 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,(a,b)->(a.start-b.start));
int max=0;
PriorityQueue<Interval> queue=new PriorityQueue<>(intervals.length,(a,b)->(a.end-b.end));
for(int i=0;i<intervals.length;i++){
while(!queue.isEmpty()&&intervals[i].start>=queue.peek().end) queue.poll();
queue.offer(intervals[i]);
max=Math.max(max,queue.size());
}
return max;
}
}
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 endsItr = 0;
for(int i=0; i<starts.length; i++) {
if(starts[i]<ends[endsItr])
rooms++;
else
endsItr++;
}
return rooms;
}
}
网友评论