Meeting Rooms
Decide whether one person can go to every meeting on a list, with no two meetings at the same time.
Each meeting is written as a pair of times [start, end]intervalstruefalse
Example 1
Output: false
Example 2
Output: true
The Idea
Picture a day in your calendar with several meetings booked. You can only sit in one of them at a time, so the day works only if each meeting is over before the next one begins. A stretch of time with a start and an end, such as [5, 10]
In Example 1 the meeting [0, 30][5, 10][0, 30]false
Spotting that clash by eye was easy with three meetings. With many meetings in no particular order, though, which ones should we compare? Comparing every meeting with every other one works, but the number of comparisons grows quickly.
A calendar app does something simpler: it shows the meetings in the order they start. Look at the picture below, where each meeting is a bar on a time line, sorted by its start time.
In the top part, labelled "Example 1, sorted by start", the dashed orange line marks time 5. That is where [5, 10][0, 30][2, 4][7, 10]
Once the meetings are in start order, each one only has to be compared with the meeting right before it. If every meeting starts at or after the end of the one before it, the meetings follow each other like beads on a string, and no two of them can overlap anywhere.
Solution Approach
Turning that idea into code means settling four things: why comparing every pair is slow, how to put the meetings in start order, the exact test between two neighbouring meetings, and how to leave the caller's array as it was.
Comparing every pair
What would the plain way cost? Two loops, one inside the other, can compare each meeting with every later one. For n meetings that is n(n - 1)/2 comparisons: 3 for Example 1, but about 500,000 for 1,000 meetings. Sorting first brings the work down to the cost of the sort plus one pass.
Putting the meetings in start order
How does Java sort an array of pairs? Arrays.sort
Comparator<int[]> byStart = (a, b) -> Integer.compare(a[0], b[0]);
Why is the meeting right before enough? Suppose every meeting starts at or after the end of the one before it. Each meeting also ends at or after its own start. So the end times only ever grow along the sorted list, and any later meeting starts after the current one has ended.
The test between two neighbours
When do two neighbours clash? The later meeting clashes if it starts strictly before the earlier one ends. Starting exactly at the end is fine, because the problem says that touching meetings do not overlap, so the test uses <<=
Leaving the input unchanged
Does the method need to change the caller's array? Sorting in place would reorder it, which can surprise whoever passed it in. Sorting a copy made with intervals.clone()
Java Solution
The method canAttendMeetings
- First, copy the array with
intointervals.clone() , so the caller's array keeps its order.sorted - Sort
by start time withsorted and the comparatorArrays.sort .(a, b) -> Integer.compare(a[0], b[0]) - A loop moves
from 1 to the last position. For eachi , it compares the start of meetingi ,i , with the end of the meeting before it,sorted[i][0] . If the start is smaller, the two clash and the method returnssorted[i - 1][1] .false - If the loop gets through every neighbour without a clash, the method returns
. With zero meetings or one meeting the loop never runs, so the answer istrue as well.true
The main
import java.util.Arrays;
public class MeetingRooms {
public static boolean canAttendMeetings(int[][] intervals) {
int[][] sorted = intervals.clone();
Arrays.sort(sorted, (a, b) -> Integer.compare(a[0], b[0])); // by start time
for (int i = 1; i < sorted.length; i++) {
if (sorted[i][0] < sorted[i - 1][1]) {
return false; // starts before the previous one ends
}
}
return true;
}
public static void main(String[] args) {
int[][] example1 = {{0, 30}, {5, 10}, {15, 20}};
System.out.println(canAttendMeetings(example1));
System.out.println(canAttendMeetings(new int[][] {{7, 10}, {2, 4}}));
System.out.println(canAttendMeetings(new int[][] {{1, 5}, {5, 8}}));
System.out.println(canAttendMeetings(new int[][] {{9, 12}, {1, 3}, {3, 6}, {6, 9}}));
System.out.println(canAttendMeetings(new int[][] {}));
System.out.println(canAttendMeetings(new int[][] {{4, 6}}));
System.out.println(Arrays.deepToString(example1));
}
}
Running it prints:
false
true
true
true
true
true
[[0, 30], [5, 10], [15, 20]]
The opening false[5, 10][0, 30]true[7, 10][2, 4]
The third line, true[1, 5][5, 8]<<=false
The fourth line is also true[1, 3][3, 6][6, 9][9, 12][4, 6]true
Complexity
The work is the sort, plus one pass that compares each meeting with the one before it.
- Time: O(n log n), because sorting the n meetings by start time costs O(n log n), and the pass that follows looks at each meeting once.
- Space: O(n), because the copy made by
holds n references to the meetings, andclone() on an array of objects may use up to O(n) extra working memory too.Arrays.sort
When the meetings may overlap and the question is how many rooms they need, see