Merge interval pattern: Check if person can attend all the meetings

Problem Statement:

You are given an array where in arr[s][e], will represent the start and end time of the meeting.

You need to check if it is possible for a sing person to attend all the meetings.

A person can attend all the meetings, if there are no overlap of the meetings.

Example:

Input:  [[2, 4], [1, 2], [7, 8]]
Output: True

Explanation: All the meeting do not overlap, hence true.

Solution Explanation: Sorting approach

Sort the meetings based on the start times.

Then compare the meeting end time with the next meeting start time to check for overlap.

Then return true or false based on the result.

Time Complexity: O(n logn)
Space Complexity: O(1)

Code Solution

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void solution(vector<vector<int>>& arr)
{
	sort(arr.begin(), arr.end());

	for (int i = 1; i < arr.size(); i++) {
        if(arr[i][1] > arr[i + 1][0]){
        	cout<<"False";
            return;
        }
    }
    cout<<"True";
    return;
}

int main()
{
    vector<vector<int>> arr = {{2, 4}, {1, 2}, {7, 8}};
    solution(arr);
}

Output

True
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *