252_Meeting Rooms

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.

For example, Given[[0, 30],[5, 10],[15, 20]], returnfalse.

Solution 1:

Sort the intervals by start time, then compare if a previous end time is later than next start time.

# Definition for an interval.
# class Interval(object):
#     def __init__(self, s=0, e=0):
#         self.start = s
#         self.end = e

class Solution(object):
    def canAttendMeetings(self, intervals):
        """
        :type intervals: List[Interval]
        :rtype: bool
        """
        # sort the interval by start time
        intervals = sorted(intervals, key=lambda interval:interval.start)
        for i in range(1, len(intervals)):
            if intervals[i].start < intervals[i-1].end:
                return False
        return True

Solution 2:

Extract all the start time and end time in two arrays, then sort each. Compare end time with next start time, if it's latter than next time then return False.

class Solution(object):
    def canAttendMeetings(self, intervals):
        """
        :type intervals: List[Interval]
        :rtype: bool
        """
        start = []
        end = []
        for i in intervals:
            start.append(i.start)
            end.append(i.end)
        start = sorted(start)
        end = sorted(end)
        for j in range(1, len(start)):
            if start[j] < end[j-1]:
                return False
        return True

Last updated