920. 会议室
中文English
给定一系列的会议时间间隔,包括起始和结束时间[[s1,e1],[s2,e2],…(si < ei),确定一个人是否可以参加所有会议。
样例
样例1
输入: intervals = [(0,30),(5,10),(15,20)]
输出: false
解释:
(0,30), (5,10) 和 (0,30),(15,20) 这两对会议会冲突
样例2
输入: intervals = [(5,8),(9,15)]
输出: true
解释:
这两个时间段不会冲突
注意事项
(0,8),(8,10)在8这这一时刻不冲突
""" Definition of Interval. class Interval(object): def __init__(self, start, end): self.start = start self.end = end """ class Solution: """ @param intervals: an array of meeting time intervals @return: if a person could attend all meetings """
def canAttendMeetings(self, intervals): # Write your code here if intervals == []: return True intervals = sorted(intervals,key = lambda num: num.start) start = intervals[0].start end = intervals[0].end for i in range(1,len(intervals)): if intervals[i].start < end: return False start = intervals[i].start end = intervals[i].end return True
大致思路:
1.判断时间之间是否重合,如果重合的话,直接返回False,否则true
最简写法:
""" Definition of Interval. class Interval(object): def __init__(self, start, end): self.start = start self.end = end """ class Solution: """ @param intervals: an array of meeting time intervals @return: if a person could attend all meetings """ def canAttendMeetings(self, intervals): end = 0 for interval in sorted(intervals,key = lambda num:num.start): if interval.start < end: return False end = interval.end return True