Skip to content

Instantly share code, notes, and snippets.

@DragonSSS
Last active September 7, 2020 04:01
Show Gist options
  • Select an option

  • Save DragonSSS/01221d52e4b733c31900a430011b951b to your computer and use it in GitHub Desktop.

Select an option

Save DragonSSS/01221d52e4b733c31900a430011b951b to your computer and use it in GitHub Desktop.
LeetCode 253 - Meeting Rooms II

LeetCode 253 - Meeting Rooms

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

Solution:

public class Solution {
    public int minMeetingRooms(int[][] intervals) {
        if (intervals == null || intervals.length == 0) {
            return 0;
        }

        Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
        PriorityQueue<Integer> heap = new PriorityQueue<>();
        heap.add(intervals[0][1]);

        for(int i = 1; i < intervals.length; i++) {
            if (intervals[i][0] >= heap.peek()) {
                heap.poll();
            }
            heap.add(intervals[i][1]);
        }
        return heap.size();
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment