Similar Problems

Similar Problems not available

Maximum Employees To Be Invited To A Meeting - Leetcode Solution

Companies:

LeetCode:  Maximum Employees To Be Invited To A Meeting Leetcode Solution

Difficulty: Hard

Topics: depth-first-search graph  

Problem Statement:

You are given a list of employees with their corresponding work hours. You need to schedule a meeting and invite the maximum number of employees possible. However, the total work hours of the employees invited to the meeting cannot exceed a certain threshold.

Write a function max_employee_invitees(schedule: List[Tuple[str, int]], work_hours: int) -> int that will take in a list of tuples called schedule where each tuple contains an employee’s name and work hours, and an integer work_hours, the maximum number of work hours that can be accommodated in the meeting and return the maximum number of employees that can be invited while keeping the total work hours under the threshold.

Example Input:

schedule = [('Employee A', 6), ('Employee B', 4), ('Employee C', 3), ('Employee D', 6), ('Employee E', 6)]
work_hours = 15

Example Output:

3

Explanation:

Employee A, D, and E can be invited to the meeting as their total work hours sum up to 6+6+3=15 which is equal to the maximum threshold, so the function returns 3.

Solution:

First, we need to sort the given employee schedule based on their work hours in descending order. To solve this problem, we can use two pointers, one starting from the beginning and the other one starting from the end of the list.

The idea here is to select the employee with the most number of work hours and see if adding it to the list makes the total work hours go over the maximum required hours. If it does, we drop the employee that has the least number of work hours and check again. We continue this process of adding and dropping employees until we find the maximum number of employees we can invite while keeping the total work hours under the threshold.

Here is the Python code which will implement the above approach:

from typing import List, Tuple

def max_employee_invitees(schedule: List[Tuple[str, int]], work_hours: int) -> int:
    
    # Sort the employee list based on their work hours in descending order
    schedule = sorted(schedule, key=lambda x: x[1], reverse=True)
    
    # Initialize pointers
    left, right = 0, len(schedule) - 1
    max_employees = 0
    
    # Loop through the employee list using two pointers
    while left <= right:
        total_work_hours = schedule[left][1] + schedule[right][1]
            
        # If only one employee in the list, just check if their work_hours fits in the meeting time
        if left == right:
            if total_work_hours <= work_hours:
                max_employees += 1
            break
        
        # If total work hours of both left and right pointer exceeds the threshold,
        # then we must decrement the work hours from the employee with the highest work hours
        if total_work_hours > work_hours:
            right -= 1
        else: # Else, we can add the left and right employee to the meeting
            max_employees += 2
            left += 1
            right -= 1
                
    return max_employees

We have implemented the above-mentioned algorithm using two pointers. The left pointer points at the employee with the most number of work hours, and the right pointer points at the employee with the least number of work hours.

First, we sorted the employee list based on their work hours in descending order. Then we initialized the left pointer to point at the beginning of the list and the right pointer to point at the end of the list. We also initialized a variable max_employees to keep track of the maximum number of employees that can attend the meeting.

Next, we looped through the employee list using the two pointers. We checked if the total work hours of the employees pointed by the left and right pointers are within the maximum threshold or not. If it is, then we added both employees to the meeting and increment both pointers and add 2 to the max_employees variable. If not, then we must decrement the right pointer to remove the employee with the least work hours and check again.

We terminate the loop when the left and right pointers cross or point at the same employee (which means there is only one employee left in the list). If there is only one employee left in the list, we just check if their work hours fit in the meeting time or not and update the max_employees variable accordingly.

Finally, we return the max_employees.

Complexity Analysis:

Time Complexity: sorting the array takes O(nlogn) time and then traversing the array using two pointers would take O(n) time. Hence the time complexity of the function is O(nlogn).

Space Complexity: We used only O(1) extra space, so the space complexity of the function, is O(1).

Maximum Employees To Be Invited To A Meeting Solution Code

1