π Problem Details
- Title:
252. Meeting Rooms
- Link: https://leetcode.com/problems/meeting-rooms/
- Difficulty: Easy
- Tags/Categories: Intervals
input array
intervals
representing the start and end time for meetings return true or false based on whether a person could attend all meetings
πWhat Were My Initial Thoughts?
- first need to sort all meetings in ascending order
- we must attend the meeting for the entire duration
- there must not be any overlap between any meetings, if there are we can return false
π‘ Explanation of Solution
1. sort intervals by start time
2. check for overlap between the end time of the previous interval and the start time of the current interval
β Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
π» Implementation of Solution
class Solution {
public:
bool canAttendMeetings(vector<vector<int>>& intervals) {
if (intervals.empty()) return true;
// Sort meetings based on start time
sort(intervals.begin(), intervals.end());
// Check for overlapping meetings
for (int i = 1; i < intervals.size(); i++) {
if (intervals[i][0] < intervals[i - 1][1]) {
return false; // Overlap detected
}
}
return true; // No overlaps
}
};