Imagine a company where many employees have meetings throughout the day. Each meeting has a start time and an end time. Our job is to find the moments when *every* employee is free at the same time — no one is in any meeting.
You are given a list of meetings. Each meeting is written as a pair [start, end], and we always know that the start comes before the end (si < ei).
For example:
meetings = [[1, 4], [2, 3], [3, 4], [4, 6], [8, 9]]
We want to find the gaps between meetings — the time slots where no meeting is happening at all. Those gaps are the free time.
One important rule: we only care about the empty spaces *inside* the overall span of meetings.
So we look only at the quiet moments squeezed between busy moments.
Let's lay the meetings out on a timeline:
[1, 4] → busy from 1 to 4[2, 3] → busy from 2 to 3 (this is already inside [1, 4])[3, 4] → busy from 3 to 4 (also inside [1, 4])[4, 6] → busy from 4 to 6[8, 9] → busy from 8 to 9If you combine all the overlapping and touching meetings, the busy time runs continuously from 1 all the way to 6. Then there is a quiet stretch, and the next meeting starts at 8.
So the free time is the gap from 6 to 8, written as [6, 8].
Output: [[6, 8]]
That single gap is the only time when *all* employees are guaranteed to be free.
To find the gaps, we first need to know how to join meetings that touch or overlap. The page gives us the rule:
> Two intervals [s1, e1] and [s2, e2] overlap if e1 >= s2.
In plain words: if the first meeting ends *at or after* the second meeting begins, the two meetings touch or run into each other, so we treat them as one big busy block.
This is why [1, 4], [2, 3], [3, 4], and [4, 6] all merge together — each one ends at or after the next one starts, so they form one unbroken busy period from 1 to 6.
Here is the intuition for how the function employeeFreeTime works:
e1 >= s2 rule) into single busy blocks.The starter code looks like this:
using namespace std;
class Solution {
public:
vector<vector<int>> employeeFreeTime(vect
};
The function returns a vector<vector<int>> — a list of intervals, where each inner list is one free-time slot like [6, 8].
This is a classic interval problem. The same merging idea appears again and again: scheduling rooms, booking systems, and calendars all rely on sorting intervals by start time and then deciding when one busy block ends and the next begins. Master this pattern once, and many similar problems become easy.