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.app
Example 1:ui
Input: [[0, 30],[5, 10],[15, 20]]
Output: 2
Example 2:this
Input: [[7,10],[2,4]]
Output: 1code
class Solution { public int minMeetingRooms(Interval[] intervals) { //0, 100 ---- 1 //0, 200 ---- 2 //0, 300 ---- 3 //150, 250 -- 3 //160, 260 -- 4 //210, 310 -- 4 int n = intervals.length; int[] startTimes = new int[n]; int[] endTimes = new int[n]; for (int i = 0; i < n; i++) { startTimes[i] = intervals[i].start; endTimes[i] = intervals[i].end; } Arrays.sort(startTimes); Arrays.sort(endTimes); int count = 0; int i = 0, j = 0; while (i < n && j < n) { //once overlaps, room++ if (startTimes[i] < endTimes[j]) count++; //not overlapping, release prev meeting room else j++; i++; } return count; } }
class Solution { public int minMeetingRooms(Interval[] intervals) { if (intervals == null || intervals.length == 0) return 0; Arrays.sort(intervals, (a, b) -> a.start-b.start); PriorityQueue<Interval> queue = new PriorityQueue<>((a, b) -> a.end-b.end); /*add room for the first meeting*/ queue.add(intervals[0]); for (int i = 1; i < intervals.length; i++) { Interval pre = queue.poll(); Interval cur = intervals[i]; // if new meeting time overlapped, add a new room for this meeting if (cur.start < pre.end) queue.offer(cur); // if not overlapped, no new room for the new meeting, // just update the ending time for the earliest-to-end room else pre.end = cur.end; // put this previous earliest-to-end room back back to heap, // so that in next iteration, we get the new earliest-to-end room queue.offer(pre); } return queue.size(); } }