Created
November 7, 2017 04:15
-
-
Save shailrshah/1bb78836d8f97421193a1b794340ed0a to your computer and use it in GitHub Desktop.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int minMeetingRooms(Interval[] intervals) { | |
if(intervals == null || intervals.length == 0) | |
return 0; | |
Arrays.sort(intervals, (i1, i2) -> i1.start - i2.start); | |
PriorityQueue<Interval> queue = new PriorityQueue<>(intervals.length, (i1, i2) -> i1.end-i2.end); | |
queue.offer(intervals[0]); // start with a room | |
for(int i = 1; i < intervals.length; i++) { | |
Interval interval = queue.poll(); // get interval that ends the soonest | |
if(intervals[i].start >= interval.end) // if no overlap, change the end time of the room | |
interval.end = intervals[i].end; | |
else | |
queue.offer(intervals[i]); // if overlap, add room | |
queue.offer(interval); // put room back in priority queue | |
} | |
return queue.size(); // each element in queue represents a room | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment