Leetcode 252, 253. Meeting Rooms
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)