2021-01-28 253. Meeting Rooms II

该博客介绍了一个中等难度的算法问题,涉及堆、贪婪算法和排序。给定一系列会议时间区间,目标是确定所需的最少会议室数量。通过排序会议开始时间并使用优先队列来管理结束时间,可以有效地解决这个问题。示例展示了如何用Java实现这个解决方案。
摘要由CSDN通过智能技术生成

253. Meeting Rooms II

Difficulty: Medium

Related Topics: Heap, Greedy, Sort

Given an array of meeting time intervals intervals where intervals[i] = [start<sub style="display: inline;">i</sub>, end<sub style="display: inline;">i</sub>], 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<sup>4</sup>
  • 0 <= start<sub style="display: inline;">i</sub> < end<sub style="display: inline;">i</sub> <= 10<sup>6</sup>
Solution

Language: Java

class Solution {
    public int minMeetingRooms(int[][] intervals) {
        Comparator<int[]> comp = new Comparator<>() {
            public int compare(int[] a1, int[] a2) {
                return a1[0] - a2[0];
            }
        };
        
        if (intervals.length == 0) {
            return 0;
        }
        
        Arrays.sort(intervals, comp);
        
        PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>() {
              public int compare(Integer a, Integer b) {
                return a - b;
              }
            });
        
        pq.add(intervals[0][1]);
        
        for (int i = 1; i < intervals.length; i++) {
            if (intervals[i][0] >=pq.peek()) {
                pq.poll();
            }
            pq.add(intervals[i][1]);
        }
        
        return pq.size();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值