Leetcode 252. Meeting Room 253. Meeting Room II
會議室系列也是常見的問題. 感覺會議室應該是屬於區間問題.
252. 會議室
本題的思路較為簡單, 只需要一個排序即可. 感覺本題的難點即在建立一個新的排序器, Array.sort() 排序傳入新的排序規則.
class Solution {
public boolean canAttendMeetings(int[][] intervals) {
if (intervals == null || intervals.length == 0) return true;
Arrays.sort(intervals, new Comparator<int[]>(){
public int compare(int[] m1, int[] m2) {
return m1[1] - m2[1];
}
});
for(int i = 0; i < intervals.length - 1; i++) {
if (intervals[i][1] > intervals[i + 1][0]) {
return false;
}
}
return true;
}
}
253. 會議室||
其實程式設計題很多都是進行模擬, 所以自己考慮這個過程一定要很清晰. 就假設自己是安排會議的人員, 那麼第一步肯定是對會議的開始時間進行排序. 第一個會議肯定得安排一個房間, 那麼後面安排會議的時候, 肯定得看最前面的那個會議是否開完了. 所以考慮使用優先佇列來實現.
這裡還可以用一種新的方式來實現比較器 :
class Solution {
public int minMeetingRooms(int[][] intervals) {
if(intervals == null || intervals.length == 0) return 0;
Arrays.sort(intervals, (a, b) -> {return a[0] - b[0];});
Queue<Integer> queue = new PriorityQueue<Integer>();
queue.add(intervals[0][1]);
for(int i = 1; i < intervals.length; i++) {
// 這裡要加上相等的情況
if (intervals[i][0] >= queue.peek()) {
queue.poll();
}
queue.add(intervals[i][1]);
}
return queue.size();
}
}
相關文章
- Leetcode 253: meeting roomLeetCodeOOM
- [Leetcode]253. Meeting Rooms IILeetCodeOOM
- 253. Meeting Rooms IIOOM
- Leetcode Meeting room問題系列 - 2LeetCodeOOM
- Leetcode Meeting room問題系列 - 1LeetCodeOOM
- LeetCode 253. Meeting Rooms II(會議室)LeetCodeOOM
- [Leetcode] 253. Meeting Rooms II 解題報告LeetCodeOOM
- Facebook面試題 meeting rooms 求同時最多meeting room的時間面試題OOM
- 【LeetCode】253. Meeting Rooms II 解題報告(C++)LeetCodeOOMC++
- 252. Meeting RoomsOOM
- Leetcode 252. Meeting Rooms (Easy) (cpp)LeetCodeOOM
- LeetCode—253.會議室 II(Meeting Rooms II)——分析及程式碼(C++)LeetCodeOOMC++
- LeetCode 252. Meeting Rooms (Java版; Easy)LeetCodeOOMJava
- LeetCode 252. Meeting Rooms(會議室)LeetCodeOOM
- LeetCode-Meeting Rooms IILeetCodeOOM
- [LeetCode 253] Meeting Rooms IILeetCodeOOM
- *LeetCode-Meeting Rooms IILeetCodeOOM
- [leetcode] 252. Meeting Rooms 解題報告LeetCodeOOM
- Meeting Rooms IIOOM
- [LeetCode253]Meeting Rooms IILeetCodeOOM
- leetcode 掃描線專題 06-leetcode.252 meeting room 力扣.252 會議室LeetCodeOOM力扣
- 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
- LeetCode-Best Meeting PointLeetCode
- chat roomOOM
- [LintCode/LeetCode] Meeting RoomsLeetCodeOOM
- [LeetCode] Meeting Rooms 會議室LeetCodeOOM
- LeetCode 題解(254) : Meeting RoomsLeetCodeOOM
- [譯] Room ? CoroutinesOOM
- HDU 4923 Room and MoorOOM
- 人生房間Life RoomOOM
- OUTLOOK - Unable to Delete Meetingsdelete
- Room Database完全使用手冊OOMDatabase