252. Meeting Rooms
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks whether a person can attend all meetings given a list of meeting time intervals. Attending all meetings is possible if and only if none of the time intervals overlap. The core challenge is to efficiently detect any overlap between any two meetings in the list.
Solution
Meeting Rooms in Python
class Solution:
def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
# If there are 0 or 1 meetings, it's always possible to attend.
if not intervals or len(intervals) == 1:
return True
# Sort the intervals based on their start times.
# This allows us to check for overlaps in a single pass.
intervals.sort(key=lambda x: x[0])
# Iterate through the sorted intervals, starting from the second one.
for i in range(1, len(intervals)):
# Check for an overlap: if the current meeting starts before the previous one ends.
# intervals[i][0] is the start time of the current meeting.
# intervals[i-1][1] is the end time of the previous meeting.
if intervals[i][0] < intervals[i-1][1]:
# An overlap is found, so it's impossible to attend all meetings.
return False
# If the loop completes without finding any overlaps, it's possible.
return True
Meeting Rooms in Java
class Solution {
public boolean canAttendMeetings(int[][] intervals) {
// If there are 0 or 1 meetings, it's always possible to attend.
if (intervals.length <= 1) {
return true;
}
// Sort the intervals based on their start times.
// This allows us to check for overlaps in a single pass.
// The comparator (a, b) -> a[0] - b[0] sorts by the first element of each sub-array.
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
// Iterate through the sorted intervals, starting from the second one.
for (int i = 1; i < intervals.length; i++) {
// Check for an overlap: if the current meeting starts before the previous one ends.
// intervals[i][0] is the start time of the current meeting.
// intervals[i-1][1] is the end time of the previous meeting.
if (intervals[i][0] < intervals[i - 1][1]) {
// An overlap is found, so it's impossible to attend all meetings.
return false;
}
}
// If the loop completes without finding any overlaps, it's possible.
return true;
}
}
Meeting Rooms in C++
class Solution {
public:
bool canAttendMeetings(vector<vector<int>>& intervals) {
// If there are 0 or 1 meetings, it's always possible to attend.
if (intervals.size() <= 1) {
return true;
}
// Sort the intervals based on their start times.
// This allows us to check for overlaps in a single pass.
sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>& b) {
return a[0] < b[0];
});
// Iterate through the sorted intervals, starting from the second one.
for (int i = 1; i < intervals.size(); ++i) {
// Check for an overlap: if the current meeting starts before the previous one ends.
// intervals[i][0] is the start time of the current meeting.
// intervals[i-1][1] is the end time of the previous meeting.
if (intervals[i][0] < intervals[i - 1][1]) {
// An overlap is found, so it's impossible to attend all meetings.
return false;
}
}
// If the loop completes without finding any overlaps, it's possible.
return true;
}
};
Meeting Rooms in TypeScript
function canAttendMeetings(intervals: number[][]): boolean {
// If there are 0 or 1 meetings, it's always possible to attend.
if (intervals.length <= 1) {
return true;
}
// Sort the intervals based on their start times.
// This allows us to check for overlaps in a single pass.
intervals.sort((a, b) => a[0] - b[0]);
// Iterate through the sorted intervals, starting from the second one.
for (let i = 1; i < intervals.length; i++) {
// Check for an overlap: if the current meeting starts before the previous one ends.
// intervals[i][0] is the start time of the current meeting.
// intervals[i-1][1] is the end time of the previous meeting.
if (intervals[i][0] < intervals[i - 1][1]) {
// An overlap is found, so it's impossible to attend all meetings.
return false;
}
}
// If the loop completes without finding any overlaps, it's possible.
return true;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.