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

Example 2:

复制代码
Input: intervals = [[7,10],[2,4]]
Output: 1

Constraints:

复制代码
1 <= intervals.length <= 10^4
0 <= starti < endi <= 10^6

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

python3 复制代码
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

Sort + sweep

python3 复制代码
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 小时前
leetcode_383. 赎金信_java
java·数据结构·算法·leetcode
√尖尖角↑4 小时前
力扣——【1991. 找到数组的中间位置】
算法·蓝桥杯
Allen Wurlitzer5 小时前
算法刷题记录——LeetCode篇(1.8) [第71~80题](持续更新)
算法·leetcode·职场和发展
百锦再6 小时前
五种常用的web加密算法
前端·算法·前端框架·web·加密·机密
碳基学AI7 小时前
北京大学DeepSeek内部研讨系列:AI在新媒体运营中的应用与挑战|122页PPT下载方法
大数据·人工智能·python·算法·ai·新媒体运营·产品运营
独家回忆3648 小时前
每日算法-250410
算法
袖清暮雨8 小时前
Python刷题笔记
笔记·python·算法
熬夜造bug8 小时前
LeetCode Hot100 刷题笔记(1)—— 哈希、双指针、滑动窗口
笔记·leetcode·hot100
风掣长空9 小时前
八大排序——c++版
数据结构·算法·排序算法
流星白龙10 小时前
【C++算法】50.分治_归并_翻转对
c++·算法