Easy and clean Python solution


  • 0
    F

    A simple and intuitive solution. Sort the array with respect to the starting time, and then we keep multiple lists, one list for one meeting room. Loop over the array, if we can find an existing room that fits, then we append it into this room. If not, we simply create a new room.

    # Definition for an interval.
    # class Interval(object):
    #     def __init__(self, s=0, e=0):
    #         self.start = s
    #         self.end = e
    
    class Solution(object):
        def minMeetingRooms(self, intervals):
            """
            :type intervals: List[Interval]
            :rtype: int
            """
            intervals.sort(key=lambda x: x.start)
            if len(intervals) == 0:
                return 0
            result = [[intervals[0]]]
            for i in range(1, len(intervals)):
                flag = False
                for j in result:
                    if j[-1].end <= intervals[i].start:
                        j.append(intervals[i])
                        flag = True
                        break
                if not flag:
                    result.append([intervals[i]])
            return len(result)
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.