Skip to main content

Leetcode 58. Meeting Rooms II. Python (Easiest Explaination)

 58. Meeting Rooms II

Description

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

Example1

Input: intervals = [(0,30),(5,10),(15,20)]
Output: 2
Explanation:
We need two meeting rooms
room1: (0,30)
room2: (5,10),(15,20)

Example2

Input: intervals = [(2,7)]
Output: 1
Explanation: 
Only need one meeting room


Solution :

"""
Definition of Interval:
class Interval(object):
    def __init__(self, start, end):
        self.start = start
        self.end = end
"""

class Solution:
    """
    @param intervals: an array of meeting time intervals
    @return: the minimum number of conference rooms required
    """
    def min_meeting_rooms(self, intervals: List[Interval]) -> int:
       
        sl = sorted([i.start for i in intervals])
        el = sorted([i.end for i in intervals])

        ans, cnt = 0, 0

        s, e = 0, 0

        while s < len(intervals):
            if sl[s] < el[e] :
                s += 1
                cnt += 1
            else:
                e += 1
                cnt -= 1
            ans = max(ans, cnt)
           
        return ans


Explaination :





Comments