Tags: "leetcode", "two-posinter", "greedy-algorithm", access_time 1-min read

Edit this post on Github

Meeting Rooms II

Created: November 11, 2018 by [lek-tin]

Last updated: November 11, 2018

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required.

Example 1

Input: [[0, 30],[5, 10],[15, 20]]
Output: 2

Example 2

Input: [[7,10],[2,4]]
Output: 1

Solution

# Definition for an interval.
# class Interval:
#     def __init__(self, s=0, e=0):
#         self.start = s
#         self.end = e
#         |____|    |_______|
#            |_______|   |____|
#           Number of meeting rooms: 2
class Solution(object):
    def minMeetingRooms(self, intervals):
        """
        :type intervals: List[Interval]
        :rtype: int
        """
        starts, ends = [], []
        length = len(intervals)
        for i in range(length):
            starts.append(intervals[i].start)
            ends.append(intervals[i].end)

        starts.sort()
        ends.sort()

        total = 0
        end = 0
        for i in range(length):
            if starts[i] < ends[end]:
                total += 1
            else:
                end += 1
        return total