Leetcode 253: meeting room
Arrang classes, you’re gonna figure out the min classroom required.
input: [0, 30],[5, 10],[15, 20]
output: 2
Java:
/*
* public class Interval {
int start;
int end;
public Interval() {
this.start = 0;
this.end = 0;
}
public Interval(int s, int e) {
this.start = s;
this.end = e;
}
}
*/
class Solution {
public static int minMeetingRooms(Interval[] intervals) {
Arrays.sort(intervals, new Comparator<Interval>(){
@Override
public int compare(Interval i1, Interval i2) {
return i1.start - i2.start;
}
});
int room = 0;
PriorityQueue<Interval> minHeap = new PriorityQueue<Interval>((Interval i1, Interval i2) -> i1.end - i2.end);
for (Interval temp:intervals) {
minHeap.offer(temp);
if (temp.start < minHeap.peek().end) room++;
else minHeap.poll();
}
return room;
}
public static void main(String[] args) {
Interval[] intervals = {new Interval(0, 30), new Interval(5, 10), new Interval(15, 20)};
System.out.println(minMeetingRooms(intervals));
}
}
另一個騷氣的方法,將class 開始的時間map成+1, 結束的時間map成-1; 最後遍歷,和最大的時候就是min room
class Solution {
public static int minMeetingRooms(Interval[] intervals) {
Arrays.sort(intervals, ((Interval i1, Interval i2) -> i2.end - i1.end));
int[] map = new int[intervals[0].end+1];
for (Interval temp:intervals) {
map[temp.start]++;
map[temp.end]--;
}
int sum = 0, max = 0;
for (int i=0; i<map.length; i++) {
sum += map[i];
max = Math.max(sum, max);
}
return max;
}
}
相關文章
- Leetcode 252. Meeting Room 253. Meeting Room IILeetCodeOOM
- [LeetCode 253] Meeting Rooms IILeetCodeOOM
- [Leetcode]253. Meeting Rooms IILeetCodeOOM
- [LeetCode253]Meeting Rooms IILeetCodeOOM
- Leetcode Meeting room問題系列 - 2LeetCodeOOM
- Leetcode Meeting room問題系列 - 1LeetCodeOOM
- LeetCode 253. Meeting Rooms II(會議室)LeetCodeOOM
- Leetcode 253:Meeting Rooms II(超詳細的解法!!!)LeetCodeOOM
- [Leetcode] 253. Meeting Rooms II 解題報告LeetCodeOOM
- 253. Meeting Rooms IIOOM
- 【LeetCode】253. Meeting Rooms II 解題報告(C++)LeetCodeOOMC++
- Facebook面試題 meeting rooms 求同時最多meeting room的時間面試題OOM
- LeetCode—253.會議室 II(Meeting Rooms II)——分析及程式碼(C++)LeetCodeOOMC++
- Leetcode: Meeting RoomsLeetCodeOOM
- LeetCode #252 - Meeting RoomsLeetCodeOOM
- Leetcode 252 Meeting RoomsLeetCodeOOM
- [LeetCode 252] Meeting RoomsLeetCodeOOM
- LeetCode252 Meeting RoomsLeetCodeOOM
- leetcode-252-Meeting RoomsLeetCodeOOM
- LeetCode-Best Meeting PointLeetCode
- LeetCode-Meeting Rooms IILeetCodeOOM
- *LeetCode-Meeting Rooms IILeetCodeOOM
- [LintCode/LeetCode] Meeting RoomsLeetCodeOOM
- [LeetCode] Meeting Rooms 會議室LeetCodeOOM
- LeetCode 題解(254) : Meeting RoomsLeetCodeOOM
- Leetcode 252. Meeting Rooms (Easy) (cpp)LeetCodeOOM
- LeetCode 252. Meeting Rooms (Java版; Easy)LeetCodeOOMJava
- LeetCode 252. Meeting Rooms(會議室)LeetCodeOOM
- leetcode253——會議室 II——java實現LeetCodeJava
- [leetcode] 252. Meeting Rooms 解題報告LeetCodeOOM
- chat roomOOM
- [譯] Room ? CoroutinesOOM
- Meeting Rooms IIOOM
- 252. Meeting RoomsOOM
- HDU 4923 Room and MoorOOM
- 人生房間Life RoomOOM
- OUTLOOK - Unable to Delete Meetingsdelete
- leetcode-區間問題總結(56,252,253,1094,435,452,646LeetCode