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 Meeting room問題系列 - 2LeetCodeOOM
- Leetcode Meeting room問題系列 - 1LeetCodeOOM
- Leetcode 253:Meeting Rooms II(超詳細的解法!!!)LeetCodeOOM
- 253. Meeting Rooms IIOOM
- 【LeetCode】253. Meeting Rooms II 解題報告(C++)LeetCodeOOMC++
- leetcode 掃描線專題 06-leetcode.252 meeting room 力扣.252 會議室LeetCodeOOM力扣
- LeetCode—253.會議室 II(Meeting Rooms II)——分析及程式碼(C++)LeetCodeOOMC++
- LeetCode #252 - Meeting RoomsLeetCodeOOM
- [LintCode/LeetCode] Meeting RoomsLeetCodeOOM
- leetcode-252-Meeting RoomsLeetCodeOOM
- LeetCode252 Meeting RoomsLeetCodeOOM
- LeetCode 252. Meeting Rooms (Java版; Easy)LeetCodeOOMJava
- leetcode253——會議室 II——java實現LeetCodeJava
- leetcode-區間問題總結(56,252,253,1094,435,452,646LeetCode
- [譯] Room ? CoroutinesOOM
- CS253 Laboratory sessionSession
- 人生房間Life RoomOOM
- Android Architecture Components 之 Room 篇AndroidOOM
- Room Database完全使用手冊OOMDatabase
- Beta階段——第十週Scrum Meeting記錄Scrum
- LiveData + ViewModel + Room (Google 官文)+DemoLiveDataViewOOMGo
- Android Room2.0之@TypeConverters使用AndroidOOM
- Android Architecture Components Part1:RoomAndroidOOM
- [譯]從 SQLite 逐步遷移到 RoomSQLiteOOM
- [譯] 從 SQLite 逐步遷移到 RoomSQLiteOOM
- Room & Kotlin 符號的處理OOMKotlin符號
- Alpha迭代階段——第七週Scrum Meeting記錄Scrum
- Android Room 之儲存 Objects 中的 ListAndroidOOMObject
- 【譯】遷移到Room的7個步驟OOM
- Android—Room資料庫多表查詢(Relationships)AndroidOOM資料庫
- RxCache 整合 Android 的持久層框架 greenDAO、RoomAndroid框架OOM
- 深入探討 Room 2.4.0 的最新進展OOM
- 最新丨CRM 整合 Meeting, 開啟新的會議體驗
- RetailX:2023年全球電子商務報告(253頁)AI
- PostgreSQL 原始碼解讀(253)- PG 14(Improving connection scalability)#5SQL原始碼
- MVVM的資料持久化(一)——ROOM的整合MVVM持久化OOM