1. 程式人生 > >Leetcode 252, 253. Meeting Rooms

Leetcode 252, 253. Meeting Rooms

eal per false app mes art 安排 一個 att

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings.

For example,
Given [[0, 30],[5, 10],[15, 20]],
return false.

所有meeting time按照start排序之後,逐個檢查當前的start是不是小於上一個end。如果是的話輸出False

 1 # Definition for an interval.
2 # class Interval(object): 3 # def __init__(self, s=0, e=0): 4 # self.start = s 5 # self.end = e 6 7 class Solution(object): 8 def canAttendMeetings(self, intervals): 9 """ 10 :type intervals: List[Interval] 11 :rtype: bool 12 """ 13 if
not intervals: 14 return True 15 16 intervals.sort(key=lambda x:x.start) 17 18 for i, x in enumerate(intervals): 19 if i>0 and x.start < intervals[i-1].end: 20 return False 21 22 return True

第二題,要求需要多少間會議室才能滿足要求。考慮real life中安排會議的方法。先按會議的開始時間排序。然後逐個安排。如果當年要安排的會議的開始時間在某個會議的結束時間之後,那麽就可以用那一間會議室,而不用另開房間。如果當前要安排的會議的開始時間比所有房間的結束時間都早,那麽需要另開一個新房間。註意在安排的新的會議之後,要更新這個房間的結束時間。註意L25的break,這說明如果已經為當前要安排的會議找到了一間,那麽就不需要再看其他的房間了。

 1 # Definition for an interval.
 2 # class Interval(object):
 3 #     def __init__(self, s=0, e=0):
 4 #         self.start = s
 5 #         self.end = e
 6 
 7 class Solution(object):
 8     def minMeetingRooms(self, intervals):
 9         """
10         :type intervals: List[Interval]
11         :rtype: int
12         """
13         if not intervals:
14             return 0
15             
16         intervals.sort(key=lambda x:x.start)
17         endTimes = []
18         
19         for intv in intervals:
20             newroom = True
21             for i, x in enumerate(endTimes):
22                 if intv.start >= x:
23                     endTimes[i] = intv.end
24                     newroom = False
25                     break
26             
27             if newroom == True:
28                 endTimes.append(intv.end)
29         
30             
31         return len(endTimes)

Leetcode 252, 253. Meeting Rooms