[Leetcode]253. Meeting Rooms II
因為Meeting Room太簡單所以我直接跳過了。那一題就是找有沒有overlap而已,根據start time排序然後看看哪個有沒有end time大於之後的start time即可。
這一題就不太一樣了。這一題,本質也是算overlap,只是它算的是存在overlap的區間裡最多overlap了幾個。
做法還是涉及到排序。但是要有兩個排序,start time一個排序, end time一個排序。或者理解為混著一起排序就可以了。
假設有一個x軸上面排著這些時間,有一個計數器,從左往右掃,遇到start time的時候就表示要開一個房間了,計數器加一。遇到end time的時候就表示可以關掉一個房間了,計數器減一。不需要考慮哪個end time對應的是哪個start time。所以這一題的結果其實就是在這個掃的過程裡,這個計數器的峰值。
根據上述原理,給出程式碼如下:
public int minMeetingRooms(Interval[] intervals) {
int[] starts = new int[intervals.length];
int[] ends = new int[intervals.length];
for (int i = 0; i < starts.length; i++) {
starts[i] = intervals[i].start;
ends[i] = intervals[i].end;
}
Arrays.sort(starts);
Arrays.sort(ends);
int count = 0;
int result = 0;
for (int i = 0, j = 0; i < starts.length;) {
if (starts[i] < ends[j]) {
count++;
i++;
result = Math.max(result, count);
} else {
j++;
count--;
}
}
return result;
}
相關文章
- 253. Meeting Rooms IIOOM
- LeetCode 253. Meeting Rooms II(會議室)LeetCodeOOM
- [Leetcode] 253. Meeting Rooms II 解題報告LeetCodeOOM
- 【LeetCode】253. Meeting Rooms II 解題報告(C++)LeetCodeOOMC++
- LeetCode—253.會議室 II(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
- [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
- 252. Meeting RoomsOOM
- [leetcode] 252. Meeting Rooms 解題報告LeetCodeOOM
- Facebook面試題 meeting rooms 求同時最多meeting room的時間面試題OOM
- Leetcode 253: meeting roomLeetCodeOOM
- LeetCode-Best Meeting PointLeetCode
- Leetcode Meeting room問題系列 - 2LeetCodeOOM
- Leetcode Meeting room問題系列 - 1LeetCodeOOM
- [LeetCode] Jump Game IILeetCodeGAM
- Leetcode jump Game IILeetCodeGAM
- Leetcode Spiral Matrix IILeetCode
- Leetcode Path Sum IILeetCode
- Leetcode-Subsets IILeetCode
- Leetcode-Permutations IILeetCode
- Leetcode Unique Paths IILeetCode