Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...]
(si < ei), determine if a person could attend all meetings.
Example 1:
Input: intervals = [[0,30],[5,10],[15,20]]
Output: false
Example 2:
Input: intervals = [[7,10],[2,4]]
Output: true
Constraints:
0 <= intervals.length <= 104
intervals[i].length == 2
0 <= starti < endi <= 106
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you're checking if a group of people can all attend a meeting without any scheduling conflicts. The brute force method looks at every single possible pair of meetings and checks if they overlap. It does this for all pairs of meetings to see if there are any conflicts.
Here's how the algorithm would work step-by-step:
def can_attend_meetings_brute_force(intervals):
number_of_meetings = len(intervals)
for first_meeting_index in range(number_of_meetings):
for second_meeting_index in range(first_meeting_index + 1, number_of_meetings):
# Iterate through all meeting pairs
start_time_first_meeting = intervals[first_meeting_index][0]
end_time_first_meeting = intervals[first_meeting_index][1]
start_time_second_meeting = intervals[second_meeting_index][0]
end_time_second_meeting = intervals[second_meeting_index][1]
# Check for overlap
if (start_time_first_meeting < end_time_second_meeting and start_time_second_meeting < end_time_first_meeting):
# If overlap, return immediately
return False
# No overlaps found, return True
return True
To find the minimum number of meeting rooms needed, think about the meetings as events that start and end at certain times. The key idea is to process these events in order of time, tracking how many meetings are happening simultaneously.
Here's how the algorithm would work step-by-step:
def min_meeting_rooms(intervals):
start_times = sorted([interval[0] for interval in intervals])
end_times = sorted([interval[1] for interval in intervals])
rooms_in_use = 0
max_rooms = 0
start_pointer = 0
end_pointer = 0
# Iterate through start and end times.
while start_pointer < len(intervals):
# If a meeting starts before the earliest ending meeting.
if start_times[start_pointer] < end_times[end_pointer]:
rooms_in_use += 1
#Keep track of the max rooms needed
max_rooms = max(max_rooms, rooms_in_use)
start_pointer += 1
else:
# Free up a room when a meeting ends.
rooms_in_use -= 1
end_pointer += 1
# The maximum number of meeting rooms used concurrently.
return max_rooms
Case | How to Handle |
---|---|
Null or Empty input array | Return true immediately, as there are no meetings to conflict with each other. |
Array with only one meeting interval | Return true immediately, as there is only one meeting and no others to conflict with. |
Meeting intervals with identical start and end times (zero duration) | The sorting algorithm handles these intervals without issues, and comparisons treat them like any other interval. |
Meeting intervals are perfectly overlapping (same start and end times for all) | The sorting will group them, and comparisons during overlap checking will identify the conflicts. |
Large number of meeting intervals to test for time complexity (scalability) | Sorting the intervals is O(n log n), and the overlap check is O(n), resulting in O(n log n) overall complexity. |
Meeting intervals with start/end times at the integer limits (Integer.MAX_VALUE, Integer.MIN_VALUE) | Ensure comparisons do not result in integer overflow by using appropriate comparison methods or data types. |
Meeting intervals are sorted (already in increasing order of start time) | The algorithm should still execute correctly and efficiently, as the sorting step will be quick. |
Meeting intervals with same start time but different end times | Sorting will order based on start time, and in case of ties, the language's sort will use a stable algorithm (as guaranteed for java.util.Arrays.sort for object arrays) maintaining original relative order, which should not impact the overlap detection logic. |