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
相关推荐
sp_fyf_202419 分钟前
计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-11-01
人工智能·深度学习·神经网络·算法·机器学习·语言模型·数据挖掘
香菜大丸40 分钟前
链表的归并排序
数据结构·算法·链表
jrrz082840 分钟前
LeetCode 热题100(七)【链表】(1)
数据结构·c++·算法·leetcode·链表
oliveira-time1 小时前
golang学习2
算法
面试鸭1 小时前
离谱!买个人信息买到网安公司头上???
java·开发语言·职场和发展
南宫生2 小时前
贪心算法习题其四【力扣】【算法学习day.21】
学习·算法·leetcode·链表·贪心算法
懒惰才能让科技进步2 小时前
从零学习大模型(十二)-----基于梯度的重要性剪枝(Gradient-based Pruning)
人工智能·深度学习·学习·算法·chatgpt·transformer·剪枝
Ni-Guvara3 小时前
函数对象笔记
c++·算法
测试19983 小时前
2024软件测试面试热点问题
自动化测试·软件测试·python·测试工具·面试·职场和发展·压力测试
泉崎3 小时前
11.7比赛总结
数据结构·算法