Leetcode 253:Meeting Rooms II(超詳細的解法!!!)
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei)
, find the minimum number of conference rooms required.
For example,
Given [[0, 30],[5, 10],[15, 20]],
return 2.
解題思路
這個問題有一個非常簡單的處理思路,我們可以先將上述的區間在座標軸上畫好,然後通過垂直於x
軸的線從左向右移動,移動的過程中,記錄這根線和區間相交的最大交點個數,這個最大交點個數就是相交的最大區間個數。
演算法的實現上我們有一個trick
,我們遍歷intervals
中的每一項it
,然後對於左邊的座標用[it[0], 1]
表示(表示我們進入一個線段),右邊的座標用[it[1], -1]
表示(表示我們退出了一個線段),然後將這些新得到的區間加入到一個tmp
陣列中,對這個陣列排序,接著遍歷這個陣列,遍歷的過程中累加我們建立的標記位(也就是前面建立的1
和-1
)記錄累加的最大值即可。
class Solution:
def minMeetingRooms(self, intervals):
if not intervals:
return 0
tmp = sorted(x for i, j in intervals for x in [[i, 1], [j, -1]])
res, n = 0, 0
for i, v in tmp:
n += v
res = max(res, n)
return res
我將該問題的其他語言版本新增到了我的GitHub Leetcode
如有問題,希望大家指出!!!
相關文章
- [LeetCode 253] Meeting Rooms IILeetCodeOOM
- [Leetcode]253. Meeting Rooms IILeetCodeOOM
- [LeetCode253]Meeting Rooms IILeetCodeOOM
- 253. Meeting Rooms IIOOM
- LeetCode 253. Meeting Rooms II(會議室)LeetCodeOOM
- [Leetcode] 253. Meeting Rooms II 解題報告LeetCodeOOM
- 【LeetCode】253. Meeting Rooms II 解題報告(C++)LeetCodeOOMC++
- LeetCode-Meeting Rooms IILeetCodeOOM
- *LeetCode-Meeting Rooms IILeetCodeOOM
- LeetCode—253.會議室 II(Meeting Rooms II)——分析及程式碼(C++)LeetCodeOOMC++
- Meeting Rooms IIOOM
- Leetcode 252. Meeting Room 253. Meeting Room IILeetCodeOOM
- Leetcode: Meeting RoomsLeetCodeOOM
- LeetCode #252 - Meeting RoomsLeetCodeOOM
- Leetcode 252 Meeting RoomsLeetCodeOOM
- [LeetCode 252] Meeting RoomsLeetCodeOOM
- Leetcode 253: meeting roomLeetCodeOOM
- LeetCode252 Meeting RoomsLeetCodeOOM
- leetcode-252-Meeting RoomsLeetCodeOOM
- [LintCode/LeetCode] Meeting RoomsLeetCodeOOM
- [LeetCode] Meeting Rooms 會議室LeetCodeOOM
- LeetCode 題解(254) : Meeting RoomsLeetCodeOOM
- Leetcode 960:刪列造序 III(超詳細的解法!!!)LeetCode
- Leetcode 252. Meeting Rooms (Easy) (cpp)LeetCodeOOM
- LeetCode 252. Meeting Rooms (Java版; Easy)LeetCodeOOMJava
- LeetCode 252. Meeting Rooms(會議室)LeetCodeOOM
- 252. Meeting RoomsOOM
- Leetcode 137:只出現一次的數字 II(最詳細的解法!!!)LeetCode
- [leetcode] 252. Meeting Rooms 解題報告LeetCodeOOM
- Leetcode 169:求眾數(最詳細的解法!!!)LeetCode
- Facebook面試題 meeting rooms 求同時最多meeting room的時間面試題OOM
- leetcode253——會議室 II——java實現LeetCodeJava
- Leetcode 通過率最高的困難題 N皇后 II 【回溯解法-剪枝】LeetCode
- 超級詳細Tcpdump 的用法TCP
- JWT 超詳細分析JWT
- LeetCode-Best Meeting PointLeetCode
- GitHub和Git超超超詳細使用教程!Github
- 超詳細的Eureka原始碼解析原始碼