Meeting Rooms II

本文介绍了一种算法,用于计算给定一系列会议时间安排时所需的最小会议室数量。通过自定义排序和优先队列来高效地解决该问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1 /**
 2  * Definition for an interval.
 3  * public class Interval {
 4  *     int start;
 5  *     int end;
 6  *     Interval() { start = 0; end = 0; }
 7  *     Interval(int s, int e) { start = s; end = e; }
 8  * }
 9  */
10 public class Solution {
11     public int minMeetingRooms(Interval[] intervals) {
12         if (intervals.length < 2) {
13             return intervals.length;
14         }
15         
16         Arrays.sort(intervals, new Comparator<Interval>() {
17             @Override
18             public int compare(Interval a, Interval b) {
19                 return a.start - b.start;
20             }
21         });
22         
23         PriorityQueue<Interval> queue = new PriorityQueue<>(intervals.length, new Comparator<Interval>() {
24            @Override
25            public int compare(Interval a, Interval b) {
26                return a.end - b.end;
27            }
28         });
29         
30         queue.offer(intervals[0]);
31         for (int i = 1; i < intervals.length; i++) {
32             Interval current = queue.poll();
33             if (intervals[i].start >= current.end) {
34                 current.end = intervals[i].end;
35             } else {
36                 queue.offer(intervals[i]);
37             }
38             queue.offer(current);
39         }
40         return queue.size();
41     }
42 }

 

转载于:https://www.cnblogs.com/shuashuashua/p/5748873.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值