Problem Description
Explanation:
The problem "Meeting Rooms" asks us to determine if a person can attend all meetings without overlapping. We are given a list of intervals where each interval represents a meeting.
To solve this problem, we can follow these steps:
- Sort the intervals based on their start times.
- Iterate through the sorted intervals and check if the current interval's start time is less than the previous interval's end time. If true, it means there is an overlap, and the person cannot attend all meetings.
The time complexity of this approach is O(nlogn) due to sorting, where n is the number of intervals. The space complexity is O(1) as we are not using any extra space.
:
Solutions
class Solution {
public 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;
}
}
Related LeetCode Problems
Loading editor...