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
相关推荐
花开富贵ii5 分钟前
代码随想录算法训练营二十八天|动态规划part01
java·数据结构·算法·leetcode·动态规划
啊阿狸不会拉杆5 分钟前
《Java 程序设计》第 7 章 - 继承与多态
java·开发语言·jvm·算法·intellij-idea
技术卷12 分钟前
详解力扣高频SQL50题之1164. 指定日期的产品价格【中等】
sql·leetcode·oracle
测试199826 分钟前
cmake应用:集成gtest进行单元测试
自动化测试·软件测试·python·测试工具·职场和发展·单元测试·测试用例
Deng9452013141 小时前
数独求解器与生成器(回溯算法实现)
算法·图形用户界面·matlab技术·数独谜题·求解器与生成器
淦暴尼1 小时前
银行客户流失预测分析
python·深度学习·算法
Swiler1 小时前
数据结构第1问:什么是数据结构?
数据结构·算法
Eloudy1 小时前
复矩阵与共轭转置矩阵乘积及其平方根矩阵
人工智能·算法·矩阵
m0_631354451 小时前
VTK开发day2:切片矩阵
人工智能·算法·矩阵
go54631584652 小时前
在本地环境中运行 ‘dom-distiller‘ GitHub 库的完整指南
人工智能·深度学习·神经网络·算法·矩阵·github