252. Meeting room

Topic
Given a meeting schedule array interval, each meeting time will include the start and end time intervals[i] = [starti, endi], please judge whether a person can participate in all the meetings inside.

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

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

提示:
0 <= intervals.length <= 104
intervals[i].length == 2
0 <= starti < endi <= 106

Topic link:
https://leetcode-cn.com/problems/meeting-rooms

Problem-solving ideas
1. Sort the list first;
2. Compare the second element x of the previous item of two adjacent items with the first element y of the next item, and if y is less than x, return False
3. If it doesn’t return False, it returns True
. 4. If the list has only one item or is empty, it returns True.

Code

class Solution:
    def canAttendMeetings(self, intervals):
        intervals.sort()
        for i in range(1,len(intervals)):
            if intervals[i][0] < intervals[i-1][1]:
                return False
        return True
        if len(intervals) == 0 or len(intervals) == 1:
            return True

s = Solution()
print(s.canAttendMeetings([[2,7],[1,5]]))

Knowledge points
If you want to sort according to the second item of the list, you can write
a.sort(key=lambda x:x[1])

Guess you like

Origin blog.csdn.net/annlin2009/article/details/114206400