Employee Free Time
Find the hours when every employee is free at once, given each employee's working hours.
Each employee's working hours are given as a list of intervals [start, end]
Example 1
Output: [[3,4]]
Example 2
Output: [[5,6],[7,9]]
The Idea
Think of three colleagues trying to book a meeting room for a talk all of them must attend. Each one has a calendar full of work blocks, and the talk can only go where none of the three calendars is blocked.
Example 2 is such a set of calendars. The first employee works [[1,3],[6,7]]
Between 1 and 5, at least one bar always covers the time. At 5 the third employee stops, and nobody starts again until the first employee at 6. So 5-6 is free for everyone. The same happens between 7 and 9. Before 1 and after 12 everyone is free too, but the stretch before 1 has no start and the one after 12 has no end, so the answer leaves them out.
Did it matter whose bar was whose? A moment is useless for the talk as soon as anybody works then, and it does not matter who. So the three rows can be squashed into one, the row labelled "someone is busy". There, 1-3, 2-4 and 2-5 melt into one block 1-5, and the free time is simply the holes between the blocks.
That is the key insight: pour every employee's intervals into one pile, merge the ones that overlap, and the gaps between the merged blocks are the answer. Merging overlapping intervals after sorting them by start is the same work as in
Solution Approach
Turning that idea into code means settling three things: how to put the intervals in time order, how to tell when a gap opens up, and which gaps to report. A faster way to get the time order, for long schedules, comes at the end as an option.
Putting the intervals in time order
How do we walk through time when the intervals sit in separate lists? Each list is sorted, but the lists interleave. Copying all of them into one list and sorting it by start time gives a single order: for Example 2, 1-3, 2-4, 2-5, 6-7, 9-12.
Telling when a gap opens
What do we need to remember while walking this list? Only the moment until which someone is busy, which is the latest end seen so far. The code keeps it in a variable named busyUntilbusyUntil
Why the latest end, and not the end of the interval just before? Because a long interval can cover several short ones after it. Take one employee working 1-10 and another working 2-3 and 5-6. After 2-3, the end of the previous interval is 3, and 5-6 starts later, but the first employee is still busy until 10. Keeping the largest end with Math.max
Which gaps to report
What about the free time before the first interval? A gap always begins at busyUntilbusyUntil
And when one interval ends exactly where the next starts, such as 1-3 and 3-4? The check is "starts after", a strict >
Optional: merging the sorted lists instead of sorting everything
Is a full sort wasteful when each employee's list is already sorted? With many intervals per employee, a little. A PriorityQueuebusyUntil
Java Solution
The method employeeFreeTime
- First, copy every employee's intervals into one list,
, with a loop over the employees. Each interval is anall of two numbers, start and end.int[] - Sort
by start time withall and a comparator that compares the first numbers.all.sort(...) - Create an empty list
for the answer, and setfree to the end of the first interval inbusyUntil .all - Walk through
with a second loop. For each interval, namedall , check whether it starts aftershift . If it does, add the gap frombusyUntil to that start tobusyUntil .free - Still inside that loop, raise
to the end ofbusyUntil if that end is later.shift - After the last interval,
holds the gaps in time order, and the method returns that list.free
Two short helpers, scheduleshowmain
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class EmployeeFreeTime {
public static List<int[]> employeeFreeTime(List<List<int[]>> schedule) {
List<int[]> all = new ArrayList<>();
for (List<int[]> employee : schedule) {
all.addAll(employee); // whose shift it is does not matter
}
all.sort((a, b) -> Integer.compare(a[0], b[0]));
List<int[]> free = new ArrayList<>();
int busyUntil = all.get(0)[1];
for (int[] shift : all) {
if (shift[0] > busyUntil) {
free.add(new int[] {busyUntil, shift[0]}); // nobody is busy in between
}
busyUntil = Math.max(busyUntil, shift[1]);
}
return free;
}
// Builds the schedule from the bracket form of the examples, one array per employee.
static List<List<int[]>> schedule(int[][]... employees) {
List<List<int[]>> result = new ArrayList<>();
for (int[][] shifts : employees) {
result.add(Arrays.asList(shifts));
}
return result;
}
static String show(List<int[]> intervals) {
List<String> parts = new ArrayList<>();
for (int[] iv : intervals) {
parts.add(Arrays.toString(iv));
}
return parts.toString();
}
public static void main(String[] args) {
System.out.println(show(employeeFreeTime(schedule(
new int[][] {{1, 2}, {5, 6}}, new int[][] {{1, 3}}, new int[][] {{4, 10}}))));
System.out.println(show(employeeFreeTime(schedule(
new int[][] {{1, 3}, {6, 7}}, new int[][] {{2, 4}}, new int[][] {{2, 5}, {9, 12}}))));
System.out.println(show(employeeFreeTime(schedule(
new int[][] {{1, 10}}, new int[][] {{2, 3}, {5, 6}}))));
System.out.println(show(employeeFreeTime(schedule(
new int[][] {{1, 3}}, new int[][] {{3, 4}}))));
System.out.println(show(employeeFreeTime(schedule(
new int[][] {{1, 2}, {4, 6}, {8, 9}}))));
}
}
Running it prints:
[[3, 4]]
[[5, 6], [7, 9]]
[]
[]
[[2, 4], [6, 8]]
Example 1 gives [[3, 4]][[5, 6], [7, 9]]
Look at the third line, the 1-10 case. It is empty because busyUntilbusyUntil = shift[1]Math.max[[3, 5]]
The fourth line is empty too: 1-3 and 3-4 touch, and the strict >
Complexity
The sort is the most expensive part; copying the intervals and the walk with busyUntil
- Time: O(N log N), because the N intervals of all employees together are sorted by start, while the copy and the walk with
do a fixed amount of work per interval.busyUntil - Space: O(N), because the pooled list
holds a reference to every interval, and the answerall can hold at most N - 1 gaps, one between each two intervals that follow each other in time order.free