0252-meeting-rooms

Try it on leetcode

Description

You are given an array of meeting times intervals where intervals[i] = [starti, endi].

A person can attend all meetings if no two meeting intervals overlap. Meetings ending at time t and starting at time t do not overlap.

​​​​​​​Return true if a person can attend all meetings. Otherwise, return false.

 

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

Solution(Python)

class Solution:
    def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
        #  intervals = [[0,30],[5,10],[15,20]]
        # 
        #  0                 30
        #       5  10
        #             15 20
        #  sort -> end
        #      s1,e1  s2,e2
        #      overlap : s2 < e1 or s1 < e2
             
        intervals.sort()
        for i in range(1, len(intervals)):
            s2, e2 = intervals[i]
            s1,e1 = intervals[i - 1]

            if s2 < e1:
                return False
        return True

        # sort the meetings by their start time. After sorting, each meeting can only overlap with the meeting immediately before it. Therefore, I only need to check whether the current meeting's start time is less than the previous meeting's end time."

Dry Run