Meeting Rooms

Updated on 12 June, 2025
Meeting Rooms header image

Problem Statement

The task is to determine whether a person can attend all specified meetings based on their given intervals. Each meeting time is described by an array where intervals[i] = [starti, endi], representing the start and end time of the i-th meeting. The objective is to identify any overlapping intervals, which would indicate that it is impossible for the person to attend all meetings simultaneously. To solve this, one needs to evaluate whether any meeting times conflict with others in the array.

Examples

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 <= 104
  • intervals[i].length == 2
  • 0 <= starti < endi <= 106

Approach and Intuition

To determine if a person can attend all the meetings without any conflicts, follow these considerations:

  1. Sort the intervals based on the start times. Sorting helps in easily comparing the end time of one meeting to the start time of the next meeting in the list.

  2. Traverse through the sorted intervals and compare the end time of the current meeting with the start time of the next meeting:

    • If the end time of one meeting is greater than the start time of the subsequent meeting, it means there is an overlap, and the person cannot attend both meetings. Hence, return false.
    • Continue this till the end of the array. If no overlaps are found, return true.

Let's apply this approach to the given examples:

  • Example 1:

    • For intervals = [[0,30],[5,10],[15,20]], when sorted by start times remains [[0,30], [5,10], [15,20]]. It's evident that the meeting from 0 to 30 overlaps with the other two meetings. Therefore, the expected outcome is false.
  • Example 2:

    • The sorted order for intervals = [[7,10],[2,4]] based on start times is [[2,4], [7,10]]. There is no overlap between [2,4] and [7,10] since the end time of the first meeting (4) is less than the start time of the second meeting (7). Hence, the expected answer is true.

This method is efficient, with the primary overhead being the time taken to sort the intervals, which is generally O(n log n). Once sorted, the check for overlap can be accomplished in linear time, O(n), leading to an overall time complexity of O(n log n). Given the constraints, this approach is computationally feasible.

Solutions

  • C++
  • Java
  • Python
cpp
class Solution {
public:
    bool isPossibleToAttend(vector<vector<int>>& timetable) {
        if (timetable.empty()) {
            return true;
        }

        // Sorting the timetable by start times
        sort(timetable.begin(), timetable.end());
        for (size_t index = 0; index < timetable.size() - 1; index++) {
            // Check for overlap
            if (timetable[index][1] > timetable[index + 1][0]) {
                return false;
            }
        }
        return true;
    }
};

The "Meeting Rooms" problem requires determining if a person can attend all meetings without any overlaps in their schedule. The solution involves checking the start and end times of the meetings.

  • Start by checking if the timetable vector is empty. If it is, return true indicating that all meetings can be attended when no meetings are scheduled.

  • Sort the timetable based on the start times of meetings using the sort() function. This ensures that the meetings are checked in the order they begin.

  • Using a for loop, iterate through the sorted timetable and compare the end time of the current meeting to the start time of the next meeting.

  • If the end time of a meeting is greater than the start time of the subsequent meeting, return false, indicating an overlap, hence it's not possible to attend all meetings.

  • If the loop completes without finding any overlaps, return true, confirming that all meetings can be attended without any conflicts.

This approach effectively determines the possibility of attending all scheduled meetings in a given timetable by leveraging sorting and sequential comparison.

java
class Solution {
    public boolean canParticipate(int[][] times) {
        Arrays.sort(times, (first, second) -> Integer.compare(first[0], second[0]));
        for (int index = 0; index < times.length - 1; index++) {
            if (times[index][1] > times[index + 1][0]) {
                return false;
            }
        }
        return true;
    }
}

The Java solution provided addresses the problem of determining if a person can attend all meetings without any overlaps. The canParticipate method takes an array times where each sub-array represents the start and end time of a single meeting.

Steps of the solution:

  1. Sort the times array based on the start time of the meetings. This helps in comparing each meeting with the next one in chronological order.
  2. Iterate through the sorted array.
  3. During each iteration, check if the current meeting's end time overlaps with the next meeting's start time.
  4. If any overlap is found, return false. This indicates that attending all meetings is not possible.
  5. If no overlaps are detected after checking all pairs of consecutive meetings, return true.

This approach ensures that the function quickly determines the feasibility of attending all the meetings, driven by efficient sorting and a single pass comparison.

python
class MeetingScheduler:
    def canSchedule(self, timeslots: List[List[int]]) -> bool:
        timeslots.sort()
        for j in range(len(timeslots) - 1):
            if timeslots[j][1] > timeslots[j + 1][0]:
                return False
        return True

The "Meeting Rooms" problem involves determining if a person can attend all meetings without any overlaps. The solution is implemented in Python and revolves around sorting the meeting intervals by their start times, then checking for overlaps.

Here's the summarized breakdown of how the solution works:

  • First, the meeting time slots are sorted based on their starting times.
  • Loop through the sorted list and check each meeting against the next one in the sequence.
  • If the end time of the current meeting overlaps with the start time of the next meeting, the function returns False indicating a scheduling conflict.
  • If no overlaps are found after checking all meeting pairs, the function returns True, confirming that all meetings can be attended without conflict.

This solution efficiently checks for possible meeting overlaps using a simplified for-loop and basic conditional statements, ensuring a straightforward approach to solving this common scheduling problem.

Comments

No comments yet.