Imagine you have a calendar full of meetings. Each meeting has a start time and an end time. A person can only be in one place at one moment, so they cannot attend two meetings that happen at the same time. Our job is to write a function that checks: can this person attend every single meeting without any clash?
If no two meetings overlap, the answer is true. If even one pair of meetings overlaps, the answer is false.
The meetings come as an array of pairs. Each pair is a small list of two numbers: the start time and the end time.
meetings = [[1, 20], [10, 30], [30, 40], [1, 5]]
Each inner list [s, e] means a meeting starts at s and ends at e. We are told that the start is always before the end (si < ei), so you will never see something silly like a meeting that ends before it begins.
So in the example above:
1 to 20.10 to 30.30 to 40.1 to 5.Two meetings overlap when one is still going on while the other has already started. Think of two people trying to use the same room at the same time — that is a clash.
This problem gives us a precise rule. For two intervals [s1, e1] and [s2, e2], they overlap when:
e1 > s2
In plain words: the first meeting ends *after* the second one has already started, so they share some time. That shared time is the clash.
There is one important edge case to notice:
> If e1 == s2, the intervals are not considered overlapping.
This means a meeting that ends exactly when the next one starts is fine. For example, a meeting ending at 30 and another starting at 30 do not clash — the person finishes the first one and walks straight into the second. The clash only happens when the end is *strictly greater* than the next start.
Input: meetings = [[1, 20], [10, 30], [30, 40], [1, 5]]
Output: false
Why false? Look at the meeting [1, 20] and the meeting [10, 30]. The first one ends at 20, but the second one already started at 10. Since 20 > 10, they share time between 10 and 20. The person would have to be in two meetings at once, which is impossible. As soon as we find even one such clash, the answer is false — we do not need to check anything else.
A clever trick makes this problem easy: sort the meetings by their start time first. Once the meetings are lined up in the order they begin, you only ever need to compare each meeting with the one right after it. If the current meeting ends after the next one starts, you have found an overlap and can return false. If you get all the way through with no clash, return true.
The function you need to write looks like this:
class Solution {
public:
bool verifySchedule(vector<vector<int>> &
};
It takes the list of meetings and returns a bool — true if the person can attend them all, false if not.
end > next start, return false.end == next start is allowed (no clash).true.