• leetcode - 253. Meeting Rooms II


    Description

    Given an array of meeting time intervals intervals where intervals[i] = [starti, endi], return the minimum number of conference rooms required.

    Example 1:

    Input: intervals = [[0,30],[5,10],[15,20]]
    Output: 2
    
    • 1
    • 2

    Example 2:

    Input: intervals = [[7,10],[2,4]]
    Output: 1
    
    • 1
    • 2

    Constraints:

    1 <= intervals.length <= 10^4
    0 <= starti < endi <= 10^6
    
    • 1
    • 2

    Solution

    Heap

    Solved after hints…

    Think about how we would approach this problem in a very simplistic way. We will allocate rooms to meetings that occur earlier in the day v/s the ones that occur later on, right?

    If you’ve figured out that we have to sort the meetings by their start time, the next thing to think about is how do we do the allocation?
    There are two scenarios possible here for any meeting. Either there is no meeting room available and a new one has to be allocated, or a meeting room has freed up and this meeting can take place there.

    So use a min-heap to store the ending time, every time we visit a new interval, compare the start time with the earliest ending time. If the start time begins later than the earliest ending time, then we could free up the room and allocate the room to the new interval. Otherwise we need to assign a new room for the new interval.

    Time complexity: o ( n log ⁡ n ) o(n\log n ) o(nlogn)
    Space complexity: o ( n ) o(n) o(n)

    Sort + sweep

    For all start, +1 at the point, and -1 for all ending points. Then sweep through all the points.

    Time complexity: o ( n log ⁡ n ) o(n\log n) o(nlogn)
    Space complexity: o ( 1 ) o(1) o(1)

    Code

    Heap

    class Solution:
        def minMeetingRooms(self, intervals: List[List[int]]) -> int:
            heap = []
            intervals.sort(key=lambda x: x[0])
            res = 0
            for i in range(len(intervals)):
                if heap and heap[0] <= intervals[i][0]:
                    heapq.heappop(heap)
                heapq.heappush(heap, intervals[i][1])
                res = max(res, len(heap))
            return res
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    Sort + sweep

    class Solution:
        def minMeetingRooms(self, intervals: List[List[int]]) -> int:
            meetings = {}
            for start, end in intervals:
                meetings[start] = meetings.get(start, 0) + 1
                meetings[end] = meetings.get(end, 0) - 1
            points = sorted(meetings)
            res = 0
            room = 0
            for each_point in points:
                room += meetings[each_point]
                res = max(res, room)
            return res
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  • 相关阅读:
    5 款漏洞扫描工具:实用、强力、全面(含开源)
    基于Spark的气象数据处理与分析
    鸿蒙应用开发-第一章-CSS3的grid布局
    pyqt5_windows_hid_auto_detect_hotplug
    TB1荧光染料
    Cyanine5.5 tetrazine|Cy5.5-tetrazine|花青素Cy5.5四嗪
    【Linux笔记】基本指令(一)
    基于C++实现⾃然连接操作算法
    【C++入门】C和C++混合编程超详细讲解
    计算机毕业设计Java“华商转转”平台的设计和实现(源码+系统+mysql数据库+lw文档)
  • 原文地址:https://blog.csdn.net/sinat_41679123/article/details/133782282