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
相关推荐
studyForMokey29 分钟前
【Android面试】四大组件专题 todo
android·面试·职场和发展
生信研究猿30 分钟前
leetcode 101.对称二叉树(不会做)
算法·leetcode·职场和发展
重生之我是Java开发战士31 分钟前
【笔试强训】Week1:点击消除,数组中两个字符串的最小距离,dd爱框框,腐烂的苹果,大数乘法
java·开发语言·算法
枫叶林FYL31 分钟前
【自然语言处理 NLP】前沿架构与多模态 选择性状态空间模型与并行扫描算法:从原理到实现
算法·自然语言处理·架构
WolfGang00732141 分钟前
代码随想录算法训练营 Day29 | 动态规划 part02
算法·动态规划
样例过了就是过了41 分钟前
LeetCode热题100 跳跃游戏 II
c++·算法·leetcode·贪心算法·动态规划
rit843249942 分钟前
基于NSGA-II的多目标优化算法(MATLAB实现)
开发语言·算法·matlab
香蕉鼠片43 分钟前
第三大的数
数据结构·算法·leetcode
汀、人工智能44 分钟前
[特殊字符] 第28课:相交链表
数据结构·算法·链表·数据库架构··相交链表
计算机安禾1 小时前
【数据结构与算法】第32篇:交换排序(一):冒泡排序
c语言·数据结构·c++·算法·链表·排序算法·visual studio code