Meeting Rooms — Solution in Python, Java & C++
Meeting Rooms solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem on arrays and sorting.
Meeting Rooms is an easy coding problem on arrays, sorting and intervals.
- Difficulty: Easy
- Topics: Arrays, Sorting, Intervals
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
Given an array of meeting time intervals ([start, end)), determine whether a person could attend all meetings — i.e. no two meetings overlap (a meeting may start exactly when another ends).
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 <= 200 <= start < end <= 100
Hints for Meeting Rooms
Hint 1
Sort by start time.
Hint 2
Any meeting starting before the previous one ends is a conflict.
How to solve Meeting Rooms
Two meetings conflict when one starts before the other ends. After sorting by start time, any conflict must involve adjacent meetings, so a single scan comparing each meeting with its predecessor is enough — no need to compare every pair.
Approach
- Sort the meetings by start time.
- Walk from the second meeting onward.
- If a meeting starts strictly before the previous one ends, they overlap — return
false. - Surviving the scan means no conflict — return
true.
Why it works
Suppose some pair overlaps. Sort them and consider the one that starts later: every meeting between them in sorted order also starts before the earlier meeting ends, so some adjacent pair overlaps too. Checking neighbours therefore catches every conflict, reducing the naive O(n^2) pairwise scan to one pass.
Complexity
- Time —
O(n log n) - Space —
O(1)
Pitfalls
- Intervals are half-open
[start, end), so a meeting starting exactly when another ends is fine — the test is strictly<. - An empty schedule is valid and returns
true. - Sorting is required; adjacency only implies conflict after sorting.
Reference solution
Python
from typing import List
def canAttendMeetings(intervals: List[List[int]]) -> bool:
s = sorted(intervals)
for i in range(1, len(s)):
if s[i][0] < s[i - 1][1]:
return False
return TrueJava
public static boolean canAttendMeetings(int[][] intervals) {
int[][] s = intervals.clone();
Arrays.sort(s, (a, b) -> a[0] - b[0]);
for (int i = 1; i < s.length; i++) {
if (s[i][0] < s[i - 1][1]) return false;
}
return true;
}C++
bool canAttendMeetings(vector<vector<int>>& intervals) {
vector<vector<int>> s = intervals;
sort(s.begin(), s.end(), [](const vector<int>& a, const vector<int>& b) { return a[0] < b[0]; });
for (int i = 1; i < (int) s.size(); i++) {
if (s[i][0] < s[i - 1][1]) return false;
}
return true;
}JavaScript
var canAttendMeetings = function(intervals) {
const s = intervals.slice().sort(function(a, b) { return a[0] - b[0]; });
for (let i = 1; i < s.length; i++) {
if (s[i][0] < s[i - 1][1]) return false;
}
return true;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 1199 arrays problems · the whole catalogue
Learn the technique: Arrays · Sorting Algorithms