253. Meeting Rooms II
Difficulty: Medium
Related Topics: Heap, Greedy, Sort
Given an array of meeting time intervals intervals where intervals[i] = [start<sub style="display: inline;">i</sub>, end<sub style="display: inline;">i</sub>], 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 <= 10<sup>4</sup>0 <= start<sub style="display: inline;">i</sub> < end<sub style="display: inline;">i</sub> <= 10<sup>6</sup>
Solution
Language: Java
class Solution {
public int minMeetingRooms(int[][] intervals) {
Comparator<int[]> comp = new Comparator<>() {
public int compare(int[] a1, int[] a2) {
return a1[0] - a2[0];
}
};
if (intervals.length == 0) {
return 0;
}
Arrays.sort(intervals, comp);
PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>() {
public int compare(Integer a, Integer b) {
return a - b;
}
});
pq.add(intervals[0][1]);
for (int i = 1; i < intervals.length; i++) {
if (intervals[i][0] >=pq.peek()) {
pq.poll();
}
pq.add(intervals[i][1]);
}
return pq.size();
}
}

该博客介绍了一个中等难度的算法问题,涉及堆、贪婪算法和排序。给定一系列会议时间区间,目标是确定所需的最少会议室数量。通过排序会议开始时间并使用优先队列来管理结束时间,可以有效地解决这个问题。示例展示了如何用Java实现这个解决方案。
468

被折叠的 条评论
为什么被折叠?



