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
相关推荐
智者知已应修善业13 分钟前
【51单片机0.1秒计时到21.0时点亮LED】2024-1-5
c++·经验分享·笔记·算法·51单片机
apcipot_rain17 分钟前
计科八股20260606——二叉树、PCA、图深度学习、进程上下文、C语言预编译、文件读写、单精度浮点数
c语言·数据结构·算法·pca·图神经网络
scx_link20 分钟前
逻辑回归的总结
算法·机器学习·逻辑回归
沐籽李33 分钟前
Proteina-Complexa:NVIDIA 如何把蛋白 Binder 设计推进到全原子生成时代?
大数据·人工智能·算法·英伟达·蛋白质生成
落羽的落羽42 分钟前
【项目】JsonRpc框架——开发实现2(业务层)
linux·数据结构·c++·人工智能·算法·json·动态规划
h_a_o777oah1 小时前
2026 蓝桥杯软件 C++B组 国赛比赛经历及备赛建议
c++·经验分享·算法·蓝桥杯
酉鬼女又兒1 小时前
零基础入门计算机网络:点对点协议PPP、媒体接入控制基本概念、静态划分信道技术、CSMA/CD与CSMA/CA协议全面详解
服务器·网络·网络协议·计算机网络·职场和发展·求职招聘·媒体
lightqjx1 小时前
【算法】数据结构_并查集
数据结构·算法·并查集
小雨下雨的雨1 小时前
鸿蒙PC Electron框架实现流体气泡模拟器
前端·人工智能·算法·华为·electron·鸿蒙
txzrxz1 小时前
广度优先搜索详解(BFS)
算法·宽度优先