253. Meeting Rooms II

本文介绍了一种高效算法,用于解决给定一系列会议时间后,如何确定最少需要多少个会议室的问题。通过使用TreeMap记录每个时间点会议室的需求变化,算法能在O(n)的时间复杂度内得出结果。

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

问题描述

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],…] (si < ei), find the minimum number of conference rooms required.

Example 1:

Input: [[0, 30],[5, 10],[15, 20]]
Output: 2

Example 2:

Input: [[7,10],[2,4]]
Output: 1

题目链接:

思路分析

给一个存储了会议开始时间和结束时间的数组,问这些会议至少需要几个会议室才能正常召开。

We use a map to store the number of meeting rooms needed. First iteration of vector intervals stores number of meeting rooms needed in a specific time. When a meeting starts, the map’s value add one. When a meeting ends, the map’s value minus one.

Then in the second iteration, we iterate the map, the initial value of rooms is zero. In a specific time point, just simply add the map’s value. If at that time one or multiple new meetings start, rooms number would add the same number. Then comparing this value with the max rooms number, we could get the final result.

代码

java

/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
class Solution {
    public int minMeetingRooms(Interval[] intervals) {
        Map<Integer, Integer> map = new TreeMap<>();
        for (Interval in : intervals){
            map.put(in.start, map.getOrDefault(in.start, 0) + 1);
            map.put(in.end, map.getOrDefault(in.end, 0) - 1);
        }
        int res = 0, rooms = 0;
        for (Integer time : map.keySet()){
            rooms += map.get(time);
            res = Math.max(res, rooms);
        }
        return res;
    }
}

C++

/**
 * Definition for an interval.
 * struct Interval {
 *     int start;
 *     int end;
 *     Interval() : start(0), end(0) {}
 *     Interval(int s, int e) : start(s), end(e) {}
 * };
 */
class Solution {
public:
    int minMeetingRooms(vector<Interval>& intervals) {
        map<int, int> roomChanges;
        for (auto i : intervals){
            roomChanges[i.start] += 1;
            roomChanges[i.end] -= 1;
        }
        int rooms = 0, maxRoom = 0;
        for (auto change : roomChanges){
            rooms += change.second;
            maxRoom = max(maxRoom, rooms);
        }
        return maxRoom;
    }
};

时间复杂度:O(n)
空间复杂度:O(n)


反思

很巧妙的把握住了整体的思想,不计较每次的变化。一定要使用TreeMap,因为hashmap是使用hash值的,遍历的时候key是没有顺序的;Treemap遍历时则会按照自然顺序排序。
HashMap 与 TreeMap的区别

Another approach. Sort the start time and end time. Then use a pointer j to denotes the last ended meeting’s end time.

Then start from the second element in start, if start after or equal end[j], it means this meeting could have in current count rooms, and j should < I for you can’t start a meeting it does not start yet. If start[I] < end[j], it means we need more rooms.

/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
class Solution {
    public int minMeetingRooms(Interval[] intervals) {
        if (intervals == null || intervals.length == 0){
            return 0;
        }
        int n = intervals.length;
        int[] start = new int[n];
        int[] end = new int[n];
        for (int i = 0; i < n; i++){
            start[i] = intervals[i].start;
            end[i] = intervals[i].end;
        }
        Arrays.sort(start);
        Arrays.sort(end);
        int count = 1;
        for (int i = 1, j = 0; i < n; i++){
            if (j < i && start[i] >= end[j]){
                j++;
            }
            else if( start[i] < end[j]){
                count++;
            }
        }
        return count;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值