Leetcode 252: Meeting Rooms

https://leetcode.com/problems/meeting-rooms/

252. Meeting Rooms

  • Total Accepted: 19160
  • Total Submissions: 43021
  • Difficulty: Easy
  • Contributors: Admin

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]],
return false.

 
 

Code

# 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
        """
        if not intervals or len(intervals) == 1:
            return True
        
        intervals = sorted(intervals, key=lambda x: x.start)
        
        for i in xrange(1, len(intervals)):
            if intervals[i].start < intervals[i-1].end:
                return False
        
        return True

 

Idea

You have to sort first which takes O(nlogn) time.

You can also look at the similar problem: https//nb4799.neu.edu/wordpress/?p=2205 

Leave a comment

Your email address will not be published. Required fields are marked *