[LeetCode] 252. Meeting Rooms_Easy tag: Sort

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

 

上一篇:【微软算法面试高频题】区间覆盖问题


下一篇:Leetcode第1288题 删除被覆盖区间C++解法