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.
252. Meeting Rooms 的拓展,同樣給一個開會的區間數組,返回最少需要的房間數。
解法1: 把區間變成2個數組:start時間數組和end時間數組,並對兩個數組排序。然后一個指針遍歷start數組,另一個指針指向end數組。如果start時間小於end時間,房間數就加1,start時間加1,比較並記錄出現過的最多房間數。start時間大於end,則所需房間數就減1,end指針加1。
解法2:最小堆minHeap,先按start排序,然后維護一個minHeap,堆頂元素是會議結束時間最早的區間,也就是end最小。每次比較top元素的end時間和當前元素的start時間,如果end < start,說明該room可以結束接下來被當前會議區間使用。最后返回堆的大小就是所需的房間數。
面試follow up: 結果要將會議名稱跟對應房間號一起返回,而不僅僅是算需要的房間數目。
Java:
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public int minMeetingRooms(Interval[] intervals) {
if(intervals == null || intervals.length == 0) return 0;
int min = 0; int max = 0;
for(int i=0; i<intervals.length; i++){
min = Math.min(min, intervals[i].start);
max = Math.max(max, intervals[i].end);
}
int[] count = new int[max-min+1];
for(int i=0; i<intervals.length; i++){
count[intervals[i].start]++;
count[intervals[i].end]--;
}
int maxroom = Integer.MIN_VALUE;
int num = 0;
for(int i=0; i<count.length; i++){
num += count[i];
maxroom = Math.max(maxroom, num);
}
return maxroom;
}
}
Java:minHeap
public class Solution {
public int minMeetingRooms(Interval[] intervals) {
int n=intervals.length;
Arrays.sort(intervals, new Comparator<Interval>(){
public int compare(Interval a, Interval b) {
return a.start-b.start;
}
});
PriorityQueue<Integer> pq=new PriorityQueue<>();
for (int i=0; i<n; i++) {
if (i>0 && intervals[i].start>=pq.peek()) pq.poll();
pq.add(intervals[i].end);
}
return pq.size();
}
}
Python:
class Solution:
# @param {Interval[]} intervals
# @return {integer}
def minMeetingRooms(self, intervals):
starts, ends = [], []
for i in intervals:
starts.append(i.start)
ends.append(i.end)
starts.sort()
ends.sort()
s, e = 0, 0
min_rooms, cnt_rooms = 0, 0
while s < len(starts):
if starts[s] < ends[e]:
cnt_rooms += 1 # Acquire a room.
# Update the min number of rooms.
min_rooms = max(min_rooms, cnt_rooms)
s += 1
else:
cnt_rooms -= 1 # Release a room.
e += 1
return min_rooms
C++:
class Solution {
public:
int minMeetingRooms(vector<Interval>& intervals) {
vector<int> starts, ends;
for (const auto& i : intervals) {
starts.emplace_back(i.start);
ends.emplace_back(i.end);
}
sort(starts.begin(), starts.end());
sort(ends.begin(), ends.end());
int min_rooms = 0, cnt_rooms = 0;
int s = 0, e = 0;
while (s < starts.size()) {
if (starts[s] < ends[e]) {
++cnt_rooms; // Acquire a room.
// Update the min number of rooms.
min_rooms = max(min_rooms, cnt_rooms);
++s;
} else {
--cnt_rooms; // Release a room.
++e;
}
}
return min_rooms;
}
};
C++: minHeap
class Solution {
public:
int minMeetingRooms(vector<Interval>& intervals) {
sort(intervals.begin(), intervals.end(), [](const Interval &a, const Interval &b){return a.start < b.start;});
priority_queue<int, vector<int>, greater<int>> q;
for (auto a : intervals) {
if (!q.empty() && q.top() <= a.start) q.pop();
q.push(a.end);
}
return q.size();
}
};
類似題目:
[LeetCode] 252. Meeting Rooms 會議室
[LeetCode] 56. Merge Intervals 合並區間
All LeetCode Questions List 題目匯總
