Meeting Rooms
Last updated
Last updated
Given an array of time intervals, determine if there's no overlap.
An overlap can happen when the start time of the next meeting is before the end time of the current meeting. Sort by the start time. Check if the current meeting start time collides with the end time in the previous meeting.
Time O(nlogn) Space O(1)
Given a list of time intervals
[[start_i, end_i], ..., [start_n, end_n]]
, find the minimum number of conferences rooms to hold all the meetings.
Notice the input is not sorted.
We can visualize this question as the picture below.
Intuitively, we want to somehow sort the meetings and find the maximum number of overlaps at some point.
The line-sweep algorithm is designed to find intersections between intervals (1D) or rectangles (2D). A brute force attempt would be to scan the entire timeline in some unit of time (i.e. in minutes) and check the overlapping of meetings at each point of time. But O(number of minutes)
is very expensive.
Is there any way to reduce the computational complexity? We don't have to scan the time frame between two events (i.e. start of a meeting, start of another meeting). Because in that time frame, the number of conference rooms stay the same. In fact, the start and the end of a meeting are the only two events that count. Therefore, we can scan these two types of events instead of the entire timeline.
If we visualize a vertical line that sweeps from left to right, at each event, the intersection count will increase by 1 when the line encounters the start of an interval and decrease by 1 when the line encounters the end of an interval. The sorting order will be ascending in time regardless of the type of the event nor the name of the meeting.
Time O(nlogn) + O(n) Space O(n)
Alternatively, we can try greedy since this is a minimization problem on a sequence.
First, we want to sort the meetings by start time (or symmetrically by end time). We can model each conference room as an interval that keeps a start time and an end time. We want to put the next meeting in the conference room with the smallest end time. If the smallest end time is bigger than the start time of next meeting, we need to create a new conference room.
Question: What if interval with different sizes have the same starting time? (i.e. a long meeting and a short meeting both start at 10). We don't care because the starting time alone can guarantee the optimality of this greedy algorithm.
Note: In Java 8, Comparator is a functional interface. It can accept lambda expression for its compare method.
Time O(nlogn) Space O(n)
A variant from Meeting Rooms II is to return all the intersection time between two lists of meetings. The brute force approach is still to scan the entire timeline and find overlaps. The better approach is to sweep the events (disregard which list they are from) and store the intervals that are intercepted (from 2 start events until the next end event).
One way to prove the optimality of a greedy algorithm is to show that at every step there is no better approach than making a greedy choice locally. We can induct on the number of conference rooms. Suppose conference needs to be created for the current meeting . If we need to add a new conference room, then every existing conference room has a schedule conflict with the current meeting. We have meetings collide with the current meeting at time . This is true because the meetings are sorted by their start time (i.e. a gap then a meeting later is not possible). Regardless of what strategies we use, at least we need conferences.
Top Coder
MIT 6.046
Oreilly
Java Doc Interface