[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 likes skyline problem very much, although we could the say the problem is much easy.
The idea is to use a kind of greedy problem.
Basic idea:
We use a priority queue to record each room‘s end time, the earliest available room‘s end time is at the the minimum heap‘s top.
Now we have a new interval (meeting).
1. If the meeting‘s start time after the earilest room‘s end time. It means we could actually arrange the new meeting into this room, we have no need to open a new room.
---------------------------------------------------------------
if (intervals[i].start >= min_heap.peek()) {
    min_heap.poll();
    min_heap.offer(intervals[i].end);
}

The reason why we must append the new meeting after the earilist avaialbe room, what if there are also other rooms available at that time?
Suppose we have two meeting room, and a new meeting. And A is the earilist available room.
A [    ]           new_1[    ]
B [          ]     new_1[    ]
Wheather we add the new meeting into room A or room B, it actually would result in the same new end time for that room. And we know all other meetings must happen after the new meeting. Suppose we have a new meeting called "new_2".

iff new_1 was added into room A
A [    ]           new_1[    ]
B [          ]            new_2[       ]

iff new_2 was added into room B
A [    ]                  new_2[       ]
B [          ]     new_1[    ]

As you can see from the change!!! If we wipe out the name of each room, it actually result in same available time structure among rooms.

2. If the meeting‘s start time before the earilest room‘s end time. It means this meeting actually conflict with all other room‘s meeting, we have to open a new room.
---------------------------------------------------------------
if (intervals[i].start < min_heap.peek()) {
    min_heap.offer(intervals[i].end);
    count++;
}
---------------------------------------------------------------

Solution:

public class Solution {
    public int minMeetingRooms(Interval[] intervals) {
        if (intervals == null)
            throw new IllegalArgumentException("intervals is null");
        int len = intervals.length;
        if (len <= 1)
            return len;
        Arrays.sort(intervals, new Comparator<Interval>() {
            @Override
            public int compare(Interval a, Interval b) {
                if (a.start == b.start)
                    return a.end - b.end;
                return a.start - b.start;
            }
        });
        PriorityQueue<Integer> min_heap = new PriorityQueue<Integer> (10);
        min_heap.offer(intervals[0].end);
        int count = 1;
        for (int i = 1; i < len; i++) {
            if (intervals[i].start < min_heap.peek()) {
                min_heap.offer(intervals[i].end);
                count++;
            } else{
                min_heap.poll();
                min_heap.offer(intervals[i].end);
            }
        }
        return count;
    }
}
时间: 2024-10-12 23:48:05

[LeetCode#253] Meeting Rooms II的相关文章

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] 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的拓展,那道题只让我们是

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 252, 253. 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. 所有meeting time按照start排序之后,逐个检查当前的s

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 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的会议时间,让我们

[LeetCode#252] Meeting Rooms

Problem: 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. Analysis: The problem is

Meeting Rooms II

1 /** 2 * Definition for an interval. 3 * public class Interval { 4 * int start; 5 * int end; 6 * Interval() { start = 0; end = 0; } 7 * Interval(int s, int e) { start = s; end = e; } 8 * } 9 */ 10 public class Solution { 11 public int minMeetingRoom

[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