source: https://leetcode.com/problems/meeting-rooms-ii/description/
Meeting Rooms II
Given an array of meeting time intervals intervals where intervals[i] = [starti, endi], return the minimum number of conference rooms required.
Example 1:
Input: intervals = [[0,30],[5,10],[15,20]]
Output: 2
Example 2:
Input: intervals = [[7,10],[2,4]]
Output: 1
Constraints:
- 1 <= intervals.length <= 104
- 0 <= starti < endi <= 106
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class Solution { public int minMeetingRooms(int[][] intervals) { PriorityQueue<Integer> endTimeQueue = new PriorityQueue<>(); //sort intervals based on the start time in ascending order Arrays.sort(intervals, new Comparator<int[]>() { public int compare(int[] a, int b[]) { return a[0] - b[0]; } }); int room = 0; for(int[] interval: intervals) { //check for the conflicts while(!endTimeQueue.isEmpty() && interval[0] >= endTimeQueue.peek()) { endTimeQueue.poll(); } endTimeQueue.add(interval[1]); room = Math.max(room, endTimeQueue.size()); } return room; } } |