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.
Example 1:
Input: [[0,30],[5,10],[15,20]]
Output: false
Example 2:
Input: [[7,10],[2,4]]
Output: true
-
这道题和合并区间那道思路都是一样的。先对区间按起始时间进行排序,如:
[[4,7],[2,8],[7,20]]
排序后:
[[2,8],[4,7],[7,20]]
再将每个元素的end time与下一个元素的start time比较,如
[2,8]
的end time为8,[4,7]
的start time为7,很明显来不及参加下一场。直接返回false。
# Definition for an interval.
# class Interval:
# def __init__(self, s=0, e=0):
# self.start = s
# self.end = e
class Solution:
def canAttendMeetings(self, intervals):
"""
:type intervals: List[Interval]
:rtype: bool
"""
intervals.sort(key=lambda c: c.start) # 按起始时间进行排序
for i in range(len(intervals) - 1):
if intervals[i].end > intervals[i + 1].start: # 如果下一场来不及参加,返回false。
return False
return True