Leetcode Meeting room問題系列 - 2
上面四類咋解決呢,我發現有共同的思路。
- 首先就是按照start開始升排序,因為時間有先後循序。
sort_list = sorted(lst_time, key = lambda x: x.start)
定義時間集合list的長度為len_t, 然後迴圈去取裡面的元素(len_t -1個元素),這個元素的end與後面元素的start比較,咋比較具 體要根據不同的題目。
for i in range(len_t-1):
lst_time[i].end lst_time[i+1].start
比如問題2
lst_time[i].end > lst_time[i+1].start:
return False
return True
那麼問題2的完整程式碼是:
class Solution(object):
def canAttendMeetings(self, intervals):
"""
:type intervals: List[Interval]
:rtype: bool
"""
intervals_sorted = sorted(intervals, key = lambda x: x.start)
len_t = len(intervals_sorted)
lst_f = []
for i in range(len_t-1):
if intervals_sorted[i+1].start >= intervals_sorted[i].end:
lst_f.append(0)
else:
lst_f.append(1)
if sum(lst_f) > 0:
return False
else:
return True
比如問題 1 就是:
比如合併後的集合是<erged = []
現在迴圈取排好的時間集合。
for i in intervals:
如果merged是空的或者最後一個的end < i.start:
Merged.append(i)
或者
Merged[-1].end = max(merged[-1].end, i.end)
return Merged
所有程式碼就是:
class Solution(object):
def merge(self, intervals):
"""
:type intervals: List[Interval]
:rtype: List[Interval]
"""
intervals.sort(key=lambda x: x.start)
merged = []
for i in intervals:
if not merged or merged[-1].end < i.start:
merged.append(i)
else:
merged[-1].end = max(merged[-1].end, i.end)
return merged
對於問題3關於會議室最小數目問題,那麼怎麼解決呢。
# 首先第一步還是排序
intervals.sort(key= lambda x: x.start)
然後就是把第一個時間的end放進去
heapq.heappush(free_rooms, intervals[0].end)
然後就是迴圈取從第二個開始取。
# 這是一個堆的,堆的特點第一個值是最小的值,最後一個值是最大的值。
# 把第一個元素end放進去啦。
heapq.heappush(free_rooms, intervals[0].end)
# 迴圈遍歷從第二個元素
for i in intervals[1:]:
# 如果free_room中最小的小於start
if free_rooms[0] <= i.start:
# 刪除掉最小的
heapq.heappop(free_rooms)
# 如果不小於那就是新增進去
heapq.heappush(free_rooms, i.end)
# 最後就是房間數量
return len(free_rooms)
關於問題5也是一樣的首先就是排序,只是這個list與以前的題目list不同
ints = sorted([i for s in schedule for i in s], key=lambda x: x.start)
res, pre = [], ints[0]
for i in ints[1:]:
if i.start <= pre.end and i.end > pre.end:
pre.end = i.end
elif i.start > pre.end:
res.append(Interval(pre.end, i.start))
pre = i
return res
你發現瞭解體規律啦嗎
關於問題4
class Solution:
def partitionLabels(self, S: str) -> List[int]:
last = {c: i for i, c in enumerate(S)}
j = anchor = 0
ans = []
for i, c in enumerate(S):
j = max(j, last[c])
if i == j:
ans.append(i - anchor + 1)
anchor = i + 1
return ans
相關文章
- Leetcode Meeting room問題系列 - 1LeetCodeOOM
- Leetcode 252. Meeting Room 253. Meeting Room IILeetCodeOOM
- Leetcode 253: meeting roomLeetCodeOOM
- Facebook面試題 meeting rooms 求同時最多meeting room的時間面試題OOM
- leetcode 掃描線專題 06-leetcode.252 meeting room 力扣.252 會議室LeetCodeOOM力扣
- LeetCode 題解(254) : Meeting RoomsLeetCodeOOM
- Leetcode: Meeting RoomsLeetCodeOOM
- [leetcode] 252. Meeting Rooms 解題報告LeetCodeOOM
- LeetCode #252 - Meeting RoomsLeetCodeOOM
- Leetcode 252 Meeting RoomsLeetCodeOOM
- [LeetCode 252] Meeting RoomsLeetCodeOOM
- [Leetcode] 253. Meeting Rooms II 解題報告LeetCodeOOM
- LeetCode252 Meeting RoomsLeetCodeOOM
- leetcode-252-Meeting RoomsLeetCodeOOM
- LeetCode-Best Meeting PointLeetCode
- LeetCode-Meeting Rooms IILeetCodeOOM
- [LeetCode 253] Meeting Rooms IILeetCodeOOM
- *LeetCode-Meeting Rooms IILeetCodeOOM
- [LintCode/LeetCode] Meeting RoomsLeetCodeOOM
- [Leetcode]253. Meeting Rooms IILeetCodeOOM
- [LeetCode] Meeting Rooms 會議室LeetCodeOOM
- [LeetCode253]Meeting Rooms IILeetCodeOOM
- 【LeetCode】253. Meeting Rooms II 解題報告(C++)LeetCodeOOMC++
- Leetcode 252. Meeting Rooms (Easy) (cpp)LeetCodeOOM
- LeetCode 252. Meeting Rooms (Java版; Easy)LeetCodeOOMJava
- LeetCode 252. Meeting Rooms(會議室)LeetCodeOOM
- LeetCode問題LeetCode
- LeetCode 253. Meeting Rooms II(會議室)LeetCodeOOM
- Leetcode刷題系列彙總LeetCode
- HDU 4923 Room and Moor(瞎搞題)OOM
- Leetcode 253:Meeting Rooms II(超詳細的解法!!!)LeetCodeOOM
- leetcode題解(陣列問題)LeetCode陣列
- 前端常見相容問題系列2:視訊哪裡去了?前端
- leetcode題解(查詢表問題)LeetCode
- Bootstrap系列 -- 2. 標題boot
- leetcode.最小棧問題LeetCode
- 關於Room資料庫,拼寫模糊查詢語句遇到的問題OOM資料庫
- chat roomOOM