🧠
Sorting + Single Pass Overlap Check
💡 Sorting intervals by start time allows us to check overlaps efficiently by only comparing adjacent intervals, reducing complexity drastically.
Intuition
Sort intervals by their start time. Then, check if any interval starts before the previous interval ends. If yes, overlap exists.
Algorithm
- Sort the intervals by their start time.
- Iterate through the sorted intervals from the second interval onward.
- For each interval, check if its start time is less than the end time of the previous interval.
- If any overlap is found, return false; otherwise, return true after the loop.
💡 Sorting simplifies the problem so you only need to check consecutive intervals, making the algorithm efficient and easy to implement.
from typing import List
def can_attend_meetings(intervals: List[List[int]]) -> bool:
intervals.sort(key=lambda x: x[0])
for i in range(1, len(intervals)):
if intervals[i][0] < intervals[i-1][1]:
return False
return True
# Driver code
if __name__ == "__main__":
print(can_attend_meetings([[0,30],[5,10],[15,20]])) # False
print(can_attend_meetings([[7,10],[2,4]])) # True
Line Notes
intervals.sort(key=lambda x: x[0])Sort intervals by start time to bring overlapping intervals next to each other
for i in range(1, len(intervals))Iterate from second interval to compare with previous
if intervals[i][0] < intervals[i-1][1]Check if current interval starts before previous ends, indicating overlap
return FalseReturn false immediately if overlap found
import java.util.*;
public class MeetingRooms {
public static boolean canAttendMeetings(int[][] intervals) {
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
for (int i = 1; i < intervals.length; i++) {
if (intervals[i][0] < intervals[i - 1][1]) {
return false;
}
}
return true;
}
public static void main(String[] args) {
System.out.println(canAttendMeetings(new int[][]{{0,30},{5,10},{15,20}})); // false
System.out.println(canAttendMeetings(new int[][]{{7,10},{2,4}})); // true
}
}
Line Notes
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));Sort intervals by start time using comparator
for (int i = 1; i < intervals.length; i++) {Loop through intervals starting from second
if (intervals[i][0] < intervals[i - 1][1]) {Check if current interval overlaps with previous
return false;Return false immediately if overlap detected
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool canAttendMeetings(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>& b) {
return a[0] < b[0];
});
for (int i = 1; i < intervals.size(); i++) {
if (intervals[i][0] < intervals[i-1][1]) {
return false;
}
}
return true;
}
int main() {
vector<vector<int>> intervals1 = {{0,30},{5,10},{15,20}};
vector<vector<int>> intervals2 = {{7,10},{2,4}};
cout << boolalpha << canAttendMeetings(intervals1) << endl; // false
cout << boolalpha << canAttendMeetings(intervals2) << endl; // true
return 0;
}
Line Notes
sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>& b) {Sort intervals by start time using lambda comparator
for (int i = 1; i < intervals.size(); i++) {Iterate from second interval to check overlap
if (intervals[i][0] < intervals[i-1][1]) {Overlap check between current and previous interval
return false;Return false immediately if overlap found
function canAttendMeetings(intervals) {
intervals.sort((a, b) => a[0] - b[0]);
for (let i = 1; i < intervals.length; i++) {
if (intervals[i][0] < intervals[i - 1][1]) {
return false;
}
}
return true;
}
// Test cases
console.log(canAttendMeetings([[0,30],[5,10],[15,20]])); // false
console.log(canAttendMeetings([[7,10],[2,4]])); // true
Line Notes
intervals.sort((a, b) => a[0] - b[0]);Sort intervals by start time to line up meetings chronologically
for (let i = 1; i < intervals.length; i++) {Loop through intervals from second to last
if (intervals[i][0] < intervals[i - 1][1]) {Check if current meeting starts before previous ends
return false;Return false immediately if overlap detected
TimeO(n log n)
SpaceO(1) or O(n) depending on sorting implementation
Sorting takes O(n log n). The single pass to check overlaps is O(n). Overall complexity dominated by sorting.
💡 For n=100000, sorting is efficient and checking overlaps is linear, making this approach scalable.
Interview Verdict: Accepted / Optimal for this problem
This approach is efficient and accepted in interviews and coding platforms.