Meeting Rooms
JavaView on GFG
Advertisement
Intuition
Check if a person can attend all meetings (no overlaps). Sort by start, check for overlaps.
Algorithm
- 1Sort intervals by start. For i in 1..n-1: if intervals[i].start < intervals[i-1].end: overlap, return false.
Common Pitfalls
- •Same as LC 252. Sort by start time. Overlap if next start < previous end.
Meeting Rooms.java
Java
// Approach: Sort intervals by start time. Check if any interval starts before the previous one ends.
// Time: O(n log n) Space: O(1)
import java.util.*;
class Solution {
static boolean canAttend(int[][] arr) {
Arrays.sort(arr, (a, b) -> a[0] - b[0]);
int end = arr[0][1];
for (int i = 1; i < arr.length; i++) {
if (end > arr[i][0]) {
return false;
}
end = arr[i][1];
}
return true;
}
}Advertisement
Was this solution helpful?