# Problem 253: Meeting Rooms II

> <https://leetcode.com/problems/meeting-rooms-ii/>

![](/files/-Lpv9zChAaUvl0OcHOCi)

## 思路

* 这道题是上一题的升级：也就是说有多少个 meeting 是 confilct 的就得有多少 room。
* 还是和上一题一样，首先对所有的 meeting 按照 start 的顺序排序，然后，用一个堆来维护当前 meeting 的 end。如果有冲突，room++；如果没有冲突，说明完美解决，把当前的 end poll 出来，offer 进新的 end。

## 变形

* Google面经中一个叫做Flight Schedule的题目，其实和此题一模一样

  > Given a schedule of flights, can you write an algorithm to count the minimum number of planes needed?

```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; }
 * }
 */
public class Solution {
    public int minMeetingRooms(Interval[] intervals) {
        if (intervals == null || intervals.length == 0) {
            return 0;
        }

        Arrays.sort(intervals, new Comparator<Interval>() {
            public int compare(Interval i1, Interval i2) {
                return i1.start - i2.start;
            }
        });

        int numRoom = 1;
        PriorityQueue<Integer> maxEnd = new PriorityQueue<Integer>();
        maxEnd.offer(intervals[0].end);
        for (int i = 1; i < intervals.length; i++) {
            if (intervals[i].start < maxEnd.peek()) {
                numRoom++;
            } else {
                maxEnd.poll();
            }
            maxEnd.offer(intervals[i].end);
        }

        return numRoom;
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://liuyang89116.gitbook.io/my-leetcode-book/post_chapter_3_array/problem_253_meeting_rooms_ii.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
