leetcode - 253. Meeting Rooms II

Description

Given an array of meeting time intervals intervals where intervalsi = 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
相关推荐
All for pursuit.4 分钟前
【回溯-7】494.目标和
数据结构·c++·算法·leetcode
白杨尚青4 分钟前
C++入门篇(十三):vector(上)——动态数组:构造、空间增长与迭代器失效
开发语言·c++·笔记·算法·stl
Jasmine_llq5 分钟前
《P17461 [GESP202609 八级] 生成树计数》
算法·栈·tarjan 算法·边双连通分量·深度优先搜索 dfs·tarjan 保存当前路径节点·模乘法
Jasmine_llq7 分钟前
《P17462 [GESP202609 八级] 末班车》
算法·dijkstra 算法·最大化版本,求最长可行值·建反向图(逆图 / 反转边)·多源 dijkstra 思想·对每个终点单独跑一遍djst·离线预处理 + 在线查询
the3clipse1 小时前
H.265熵编码核心:CABAC自适应二进制算术编码详解——如何将语法元素高效压缩为比特流
人工智能·算法·视频编码·h.265·hevc·cabac·cavlc
CQU_JIAKE1 小时前
10.4【A】
算法
Godspeed Zhao1 小时前
现代智能雷达技术19——算法(5)
算法
(Charon)1 小时前
【C++面试】RAII到底是什么:从资源管理理解C++对象生命周期
c++·算法·面试
Omics Pro1 小时前
之江实验室NAR|虚拟细胞3阶段训练范式
数据库·人工智能·算法·机器学习·自然语言处理
Tisfy1 小时前
LeetCode 0678.有效的括号字符串:O(n)+O(1)一次遍历
算法·leetcode·字符串·题解·贪心·括号匹配