[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 解題報告(C++)LeetCodeOOMC++
- LeetCode—253.會議室 II(Meeting Rooms II)——分析及程式碼(C++)LeetCodeOOMC++
- Leetcode 252. Meeting Room 253. Meeting Room IILeetCodeOOM
- Leetcode 253:Meeting Rooms II(超詳細的解法!!!)LeetCodeOOM
- LeetCode #252 - Meeting RoomsLeetCodeOOM
- [LintCode/LeetCode] Meeting RoomsLeetCodeOOM
- leetcode-252-Meeting RoomsLeetCodeOOM
- LeetCode252 Meeting RoomsLeetCodeOOM
- LeetCode 252. Meeting Rooms (Java版; Easy)LeetCodeOOMJava
- Leetcode 253: meeting roomLeetCodeOOM
- Leetcode Meeting room問題系列 - 2LeetCodeOOM
- Leetcode Meeting room問題系列 - 1LeetCodeOOM
- leetcode-90. Subsets IILeetCode
- Leetcode 40 Combination Sum IILeetCode
- Leetcode 213 House Robber IILeetCode
- LeetCode 1103[分糖果II]LeetCode
- [LeetCode] 210. Course Schedule IILeetCode
- [LeetCode] 305. Number of Islands IILeetCode
- [LeetCode] 212. Word Search IILeetCode
- [LeetCode] 2105. Watering Plants IILeetCode
- LeetCode 52. N皇后 IILeetCode
- [leetcode]linked-list-cycle-iiLeetCode
- leetcode 219. Contains Duplicate IILeetCodeAI
- [LeetCode] 910. Smallest Range IILeetCode
- [LeetCode] 45. Jump Game IILeetCodeGAM
- LeetCode-047-全排列 IILeetCode
- Leetcode 137. Single Number IILeetCode
- [LeetCode] 3152. Special Array IILeetCode
- leetcode 掃描線專題 06-leetcode.252 meeting room 力扣.252 會議室LeetCodeOOM力扣
- Leetcode 685. Redundant Connection II JavascriptLeetCodeJavaScript
- [LeetCode] 244. Shortest Word Distance IILeetCode
- LeetCode - 113 - 路徑總和 IILeetCode
- leetcode 350. Intersection of Two Arrays IILeetCode
- leetcode力扣 213. 打家劫舍 IILeetCode力扣
- Leetcode 142. Linked List Cycle IILeetCode
- LeetCode-063-不同路徑IILeetCode
- leetcode:組合總和II(回溯java)LeetCodeJava