LeetCode每日一题(2402. Meeting Rooms III)

给定n个房间和一系列会议时间,每个会议会占用特定的房间,目标是确定哪个房间举办的会议最多。算法涉及对会议按开始时间排序,使用两个小堆管理空闲和占用的会议室,优先分配空闲且编号小的房间。
摘要由CSDN通过智能技术生成

You are given an integer n. There are n rooms numbered from 0 to n - 1.

You are given a 2D integer array meetings where meetings[i] = [starti, endi] means that a meeting will be held during the half-closed time interval [starti, endi). All the values of starti are unique.

Meetings are allocated to rooms in the following manner:

Each meeting will take place in the unused room with the lowest number.
If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the same duration as the original meeting.
When a room becomes unused, meetings that have an earlier original start time should be given the room.
Return the number of the room that held the most meetings. If there are multiple rooms, return the room with the lowest number.

A half-closed interval [a, b) is the interval between a and b including a and not including b.

Example 1:

Input: n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
Output: 0

Explanation:

  • At time 0, both rooms are not being used. The first meeting starts in room 0.
  • At time 1, only room 1 is not being used. The second meeting starts in room 1.
  • At time 2, both rooms are being used. The third meeting is delayed.
  • At time 3, both rooms are being used. The fourth meeting is delayed.
  • At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
  • At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
    Both rooms 0 and 1 held 2 meetings, so we return 0.

Example 2:

Input: n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
Output: 1

Explanation:

  • At time 1, all three rooms are not being used. The first meeting starts in room 0.
  • At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
  • At time 3, only room 2 is not being used. The third meeting starts in room 2.
  • At time 4, all three rooms are being used. The fourth meeting is delayed.
  • At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
  • At time 6, all three rooms are being used. The fifth meeting is delayed.
  • At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
    Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.

Constraints:

  • 1 <= n <= 100
  • 1 <= meetings.length <= 105
  • meetings[i].length == 2
  • 0 <= starti < endi <= 5 * 105
  • All the values of starti are unique.

题目不复杂, 其实就两种情况, 一种是会议等会议室,也就是会议到了开始时间没有空会议室的情况, 这种情况就是找结束时间最接近当前时间的会议室。另一种情况是会议室等会议, 也就是会议室已经有空出来的了,但是下一个会议还没到开始时间, 这种情况我们需要给会议室进行排序, 但是此时排序不是按结束时间来排,而是按会议室的编号来排, 因为题目中规定,如果有多个空的会议室, 挑号码小的使用。具体的算法如下:

  1. 对 meetings 按开始时间进行排序
  2. 创建两个 BinaryHeap(小堆), 一个保存空闲的会议室编号, 一个保存被使用的会议室的会议结束时间和会议室编号
  3. 每次取出一个会议,先从被占用的会议室里找出结束时间小于当前会议开始时间的会议室, 这些会议室在当前时间点已经是空闲的了, 我们把它们统统放进空闲会议室 heap 中, 然后我们尝试从空闲会议室中取出一个会议室, 如果有空闲会议室, 我们把当前会议的结束时间和取出的会议室编号放到占用会议室 hea
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值