04(Aug) N meetings in one room

04. N Meetings in One Room

The problem can be found at the following link: Question Link

Problem Description

You are given timings of n meetings in the form of (start[i], end[i]) where start[i] is the start time of meeting i and end[i] is the finish time of meeting i. Return the maximum number of meetings that can be accommodated in a single meeting room, when only one meeting can be held in the meeting room at a particular time. Note that the start time of one chosen meeting can't be equal to the end time of another chosen meeting.

Example:

Input:

n = 6
start[] = [1, 3, 0, 5, 8, 5]
end[] = [2, 4, 6, 7, 9, 9]

Output:

4

Explanation: Maximum four meetings can be held with given start and end timings. The meetings are - (1, 2), (3, 4), (5, 7), and (8, 9).

My Approach

  1. Initialization:

    • Create a list of meetings where each meeting is represented as a tuple of (start[i], end[i]).

    • Sort the meetings based on their end times to prioritize the meeting that ends the earliest.

  2. Meeting Selection:

    • Iterate through the sorted list of meetings.

    • Keep track of the end time of the last selected meeting.

    • For each meeting, if its start time is greater than the end time of the last selected meeting, include it in the count and update the end time.

  3. Return:

    • Return the count of meetings that can be accommodated.

Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(n log n), as we sort the meetings by their end times, and then iterate through them.

  • Expected Auxiliary Space Complexity: O(n), as we use a list to store the meetings and an additional variable for tracking the end time.

Code

C++

Java

Python

Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


📍Visitor Count

Last updated