Optimal Meeting Points

Optimal Meeting Points

December 24, 2023
medium
Greedy Algorithm

Problem #

You are given an array of pairs representing the start and end times of various meetings. Given an integer K, a person can attend at most K meetings. Find the optimal meeting points that allow the person to attend the maximum number of meetings.

Solution #

The problem is about finding the maximum number of non-overlapping meetings a person can attend, given a limit on the total number of meetings they can attend (K). This is a classic greedy algorithm problem where meetings are selected based on their end times. The goal is to choose meetings that finish earliest, as this allows for attending more meetings overall.

Here’s the Python code to solve this problem:

def max_meetings(meetings, k):
    # Sort the meetings based on their finish times
    meetings.sort(key=lambda x: x[1])

    # Initialize variables
    max_meetings_attended = []
    current_end_time = 0

    for start, end in meetings:
        if start >= current_end_time and k > 0:
            max_meetings_attended.append((start, end))
            current_end_time = end
            k -= 1

    return max_meetings_attended

# Example usage
meetings = [(1, 3), (2, 4), (3, 5), (7, 9), (8, 10)]
k = 3
optimal_meetings = max_meetings(meetings, k)
print(optimal_meetings)

In this code:

  • The meetings list is first sorted by the end time of each meeting.
  • The function then iterates through these sorted meetings, and for each meeting, it checks if the meeting starts after the end of the last selected meeting and if the limit k has not been reached.
  • If both conditions are met, the meeting is added to the max_meetings_attended list, the current_end_time is updated to the end time of the current meeting, and k is decremented.
  • Finally, the function returns the list of optimally selected meetings.

In the provided example, with meetings = [(1, 3), (2, 4), (3, 5), (7, 9), (8, 10)] and k = 3, the output will be the list of meetings [(1, 3), (3, 5), (7, 9)], indicating the maximum number of non-overlapping meetings that can be attended within the limit.

The provided Python code finds the optimal set of meetings a person can attend, given a limit on the number of meetings (K) they can attend. It aims to maximize the number of meetings within this limit.

Solution analysis #

Function max_meetings #

  • Input:

    • meetings: A list of tuples, where each tuple represents a meeting with a start and end time.
    • k: An integer representing the maximum number of meetings a person can attend.
  • Process:

    1. Sort Meetings: The meetings are sorted by their end times. This step is crucial because attending meetings that end earlier allows attending more meetings overall.
    2. Select Meetings: The function iterates through the sorted meetings and selects a meeting if its start time is after the end time of the previously selected meeting. This ensures no overlapping meetings are chosen.
    3. Respect Limit: The selection process stops when the number of selected meetings reaches k.
  • Output:

    • A list of selected meetings that maximizes the number of meetings attended without exceeding the limit k.

Example Usage #

  • The meetings list is [(1, 3), (2, 4), (3, 5), (7, 9), (8, 10)], and k is 3.
  • The function computes and returns the list [(1, 3), (3, 5), (7, 9)] as the optimal set of meetings. These are the maximum number of non-overlapping meetings that can be attended, given the limit k.

This approach is efficient and ensures that the person attends the maximum possible number of meetings without any schedule conflicts, within the specified limit.