Description
Decide whether one person can attend all meetings. Each meeting has start < end and touching endpoints are allowed.
Solution
def can_attend_meetings(intervals):
ordered = sorted(intervals)
return all(ordered[i-1][1] <= ordered[i][0] for i in range(1,len(ordered)))Examples
Inputs are positional arguments. Trees use level-order arrays; linked lists use value arrays. Design problems list operations in order.
Example 1
- Input
[[[1,3],[3,5]]]- Output
true
The first meeting ends exactly when the next begins.
Example 2
- Input
[[[1,4],[2,3]]]- Output
false
The meetings overlap between 2 and 3.
Example 3
- Input
[[]]- Output
true
No meetings cause no conflict.
Approach
Sort by start and look for a start earlier than the previous meeting's end.
Time & space
O(n log n) time; O(n) sorting space. LeetCode uses an int[][] interval input. Here n is the input length.