代码改变世界

[LeetCode] 252. Meeting Rooms_Easy tag: Sort

2021-07-29 07:24  Johnson_强生仔仔  阅读(13)  评论(0编辑  收藏  举报

Given an array of meeting time intervals where intervals[i] = [starti, endi], determine if a person could attend all meetings.

 

Example 1:

Input: intervals = [[0,30],[5,10],[15,20]]
Output: false

Example 2:

Input: intervals = [[7,10],[2,4]]
Output: true

 

Constraints:

  • 0 <= intervals.length <= 104
  • intervals[i].length == 2
  • 0 <= starti < endi <= 106

 

Ideas: 这个题目实际上跟[LeetCode] 56. Merge Intervals_Medium Tag: sort 类似, 就是看看是否有重合。

Code: 

class Solution:
    def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
        intervals.sort(key= lambda x: x[0])
        for i in range(1, len(intervals)):
            if intervals[i][0] < intervals[i - 1][1]:
                return False
        return True