[LeetCode] Meeting Rooms II 会议室之二

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.

For example,
Given [[0, 30],[5, 10],[15, 20]],
return 2.

这道题是之前那道Meeting Rooms的拓展,那道题只让我们是否能参加所有的会,也就是看会议之间有没有时间冲突,而这道题让我们求最少需要安排几个会议室,有时间冲突的肯定需要安排在不同的会议室。这道题有好几种解法,我们先来看使用map来做的,我们遍历时间区间,对于起始时间,映射值自增1,对于结束时间,映射值自减1,然后我们定义结果变量res,和房间数rooms,我们遍历map,时间从小到大,房间数每次加上映射值,然后更新结果res,遇到起始时间,映射是正数,则房间数会增加,如果一个时间是一个会议的结束时间,也是另一个会议的开始时间,则映射值先减后加仍为0,并不用分配新的房间,而结束时间的映射值为负数更不会增加房间数,利用这种思路我们可以写出代码如下:

解法一:

class Solution {
public:
    int minMeetingRooms(vector<Interval>& intervals) {
        map<int, int> m;
        for (auto a : intervals) {
            ++m[a.start];
            --m[a.end];
        }
        int rooms = 0, res = 0;
        for (auto it : m) {
            res = max(res, rooms += it.second);
        }
        return res;
    }
};

第二种方法是用两个一维数组来做,分别保存起始时间和结束时间,然后各自排个序,我们定义结果变量res和结束时间指针endpos,然后我们开始遍历,如果当前起始时间小于结束时间指针的时间,则结果自增1,反之结束时间指针自增1,这样我们可以找出重叠的时间段,从而安排新的会议室,参见代码如下:

解法二:

class Solution {
public:
    int minMeetingRooms(vector<Interval>& intervals) {
        vector<int> starts, ends;
        int res = 0, endpos = 0;
        for (auto a : intervals) {
            starts.push_back(a.start);
            ends.push_back(a.end);
        }
        sort(starts.begin(), starts.end());
        sort(ends.begin(), ends.end());
        for (int i = 0; i < intervals.size(); ++i) {
            if (starts[i] < ends[endpos]) ++res;
            else ++endpos;
        }
        return res;
    }
};

再来一看一种使用最小堆来解题的方法,这种方法先把所有的时间区间按照起始时间排序,然后新建一个最小堆,开始遍历时间区间,如果堆不为空,且首元素小于等于当前区间的起始时间,我们去掉堆中的首元素,把当前区间的结束时间压入堆,由于最小堆是小的在前面,那么假如首元素小于等于起始时间,说明上一个会议已经结束,可以用该会议室开始下一个会议了,所以不用分配新的会议室,遍历完成后堆中元素的个数即为需要的会议室的个数,参见代码如下;

解法三:

class Solution {
public:
    int minMeetingRooms(vector<Interval>& intervals) {
        sort(intervals.begin(), intervals.end(), [](const Interval &a, const Interval &b){return a.start < b.start;});
        priority_queue<int, vector<int>, greater<int>> q;
        for (auto a : intervals) {
            if (!q.empty() && q.top() <= a.start) q.pop();
            q.push(a.end);
        }
        return q.size();
    }
};

类似题目:

Meeting Rooms

参考资料:

https://leetcode.com/discuss/50948/c-o-n-log-n-584-ms-3-solutions

https://leetcode.com/discuss/71846/super-easy-java-solution-beats-98-8%25

https://leetcode.com/discuss/64686/concise-c-solution-with-min_heap-sort-greedy

LeetCode All in One 题目讲解汇总(持续更新中...)

时间: 2024-12-28 16:14:29

[LeetCode] Meeting Rooms II 会议室之二的相关文章

[LeetCode] Meeting Rooms I &amp; II

Meeting Rooms Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings. For example,Given [[0, 30],[5, 10],[15, 20]],return false. 1 /** 2 * Definition

Meeting Rooms II -- LeetCode

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. For example,Given [[0, 30],[5, 10],[15, 20]],return 2. 思路:贪心. 将所有interval按照开始时间从早到晚排序.之后

[LeetCode#253] Meeting Rooms II

Problem: 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. For example,Given [[0, 30],[5, 10],[15, 20]],return 2. Analysis: This problem l

LeetCode 252. Meeting Rooms (会议室)

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings. For example,Given [[0, 30],[5, 10],[15, 20]],return false. 题目标签:sort 这道题目给了我们一个array的会议时间,让我们

leetcode253 - Meeting Rooms II - medium

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: 2Example 2:Input: [[7,10],[2,4]]Output

Leetcode Meeting Rooms

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings. For example,Given [[0, 30],[5, 10],[15, 20]],return false. 解题思路: 先对start sort, O(nlgn), 然后比较S

253 Meeting Rooms II

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. For example, Given [[0, 30],[5, 10],[15, 20]], return 2. 看到区间求重叠的部分 , 就想到对区间排序(start, en

[LeetCode] House Robber II 打家劫舍之二

Note: This is an extension of House Robber. After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle.

[LeetCode] The Maze II 迷宫之二

There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling up, down, left or right, but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction. Given the ball's