Sunday, October 20, 2024

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

Solution

class Solution {
public int minMeetingRooms(int[][] intervals) {
// Sort intervals by start
Arrays.sort(intervals, Comparator.comparing((int[] intrvl) -> intrvl[0]));
int count=0;
PriorityQueue<Integer> heap = new PriorityQueue<>();
for(int[] intrvl : intervals) {
if(heap.isEmpty()) {
count++;
heap.add(intrvl[1]);
} else {
if(intrvl[0] >= heap.peek()) {
heap.poll();
} else {
count++;
}
heap.add(intrvl[1]);
}
}
return count;
}
}

No comments:

Post a Comment