LeetCode 253. Meeting Rooms II(會議室)
原題網址:https://leetcode.com/problems/meeting-rooms-ii/
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
.
/**
* 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) {
Arrays.sort(intervals, new Comparator<Interval>() {
@Override
public int compare(Interval i1, Interval i2) {
return Integer.compare(i1.start, i2.start);
}
});
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
int rooms = 0;
for(int i=0; i<intervals.length; i++) {
minHeap.offer(intervals[i].end);
if (intervals[i].start < minHeap.peek()) {
rooms ++;
} else {
minHeap.poll();
}
}
return rooms;
}
}
更直觀的實現方法:
/**
* 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) {
Arrays.sort(intervals, new Comparator<Interval>() {
@Override
public int compare(Interval i1, Interval i2) {
return Integer.compare(i1.start, i2.start);
}
});
int rooms = 0;
int active = 0;
PriorityQueue<Integer> heap = new PriorityQueue<>();
for(int i=0; i<intervals.length; i++) {
while (!heap.isEmpty() && heap.peek() <= intervals[i].start) {
active --;
heap.poll();
}
active ++;
heap.offer(intervals[i].end);
rooms = Math.max(rooms, active);
}
return rooms;
}
}
方法二:分別對起始時間和結束時間排序,由於會議之間並無差異(不像skyline問題,不同建築的高度不一樣),所以分別使用兩個指標來推進起始時間和結束時間。
/**
* 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) {
int[] starts = new int[intervals.length];
int[] ends = new int[intervals.length];
for(int i=0; i<intervals.length; i++) {
starts[i] = intervals[i].start;
ends[i] = intervals[i].end;
}
Arrays.sort(starts);
Arrays.sort(ends);
int rooms = 0;
int activeMeetings = 0;
int i=0, j=0;
while (i < intervals.length && j < intervals.length) {
if (starts[i] < ends[j]) {
activeMeetings ++;
i ++;
} else {
activeMeetings --;
j ++;
}
rooms = Math.max(rooms, activeMeetings);
}
return rooms;
}
}
參考文章:
http://buttercola.blogspot.com/2015/08/leetcode-meeting-rooms-ii.html
http://www.jyuan92.com/blog/leetcode-meeting-rooms-ii/
相關文章
- LeetCode—253.會議室 II(Meeting Rooms II)——分析及程式碼(C++)LeetCodeOOMC++
- [Leetcode]253. Meeting Rooms IILeetCodeOOM
- 253. Meeting Rooms IIOOM
- [LeetCode] Meeting Rooms 會議室LeetCodeOOM
- [Leetcode] 253. Meeting Rooms II 解題報告LeetCodeOOM
- LeetCode 252. Meeting Rooms(會議室)LeetCodeOOM
- 【LeetCode】253. Meeting Rooms II 解題報告(C++)LeetCodeOOMC++
- LeetCode-Meeting Rooms IILeetCodeOOM
- [LeetCode 253] Meeting Rooms IILeetCodeOOM
- *LeetCode-Meeting Rooms IILeetCodeOOM
- Meeting Rooms IIOOM
- [LeetCode253]Meeting Rooms IILeetCodeOOM
- Leetcode 252. Meeting Room 253. Meeting Room IILeetCodeOOM
- Leetcode 253:Meeting Rooms II(超詳細的解法!!!)LeetCodeOOM
- Leetcode: Meeting RoomsLeetCodeOOM
- LeetCode #252 - Meeting RoomsLeetCodeOOM
- Leetcode 252 Meeting RoomsLeetCodeOOM
- [LeetCode 252] Meeting RoomsLeetCodeOOM
- LeetCode252 Meeting RoomsLeetCodeOOM
- leetcode-252-Meeting RoomsLeetCodeOOM
- leetcode253——會議室 II——java實現LeetCodeJava
- [LintCode/LeetCode] Meeting RoomsLeetCodeOOM
- LeetCode 題解(254) : Meeting RoomsLeetCodeOOM
- Leetcode 252. Meeting Rooms (Easy) (cpp)LeetCodeOOM
- LeetCode 252. Meeting Rooms (Java版; Easy)LeetCodeOOMJava
- 252. Meeting RoomsOOM
- [leetcode] 252. Meeting Rooms 解題報告LeetCodeOOM
- [LeetCode] 252、會議室LeetCode
- LeetCode 252. 會議室(排序)LeetCode排序
- 【leetcode】252 會議室(陣列)LeetCode陣列
- LeetCode-Python-252. 會議室LeetCodePython
- Facebook面試題 meeting rooms 求同時最多meeting room的時間面試題OOM
- 每天站立會議(Daily Stand-up Meeting)AI
- Leetcode 253: meeting roomLeetCodeOOM
- 會議室多屏同步顯示
- 高階網路會議室如何實現會議集中控制
- LeetCode-Best Meeting PointLeetCode
- 小型無線會議室解決方案