Task Assignment with Skill Matching and One Flexible Worker
Greedy & IntervalsAuto-graded · 24 test casesLast updated
Tests greedy selection within independent groups and optimal placement of a single unrestricted resource.
You manage a team completing jobs for clients. Each team member has a specific skill level (an integer), and each job requires a particular skill level and pays a certain profit. A team member can only work on a job if their skill exactly matches the job's requirement. Each person completes at most one job.
Additionally, you have access to one contractor who can complete any single job regardless of its skill requirement.
Each job can be completed at most once. Some jobs may remain incomplete, and some team members may remain idle. Calculate the maximum total profit you can achieve.
Write a function that takes a list of team member skills and a list of jobs, and returns the maximum profit.
Input:
team_skills: a list whereteam_skills[i]is the skill level of team memberijobs: a list wherejobs[j] = [required_skill, profit]describes jobj
Output:
- A single integer representing the maximum total profit
Rules:
- A team member with skill
scan only be assigned a job withrequired_skill == s - The contractor can be assigned any one job, or remain unused
- Each person (team member or contractor) completes at most one job
- Each job is completed by at most one person
- Unassigned jobs and idle workers are permitted
Constraints:
1 <= len(team_skills) <= 10^51 <= team_skills[i] <= 10^91 <= len(jobs) <= 10^5len(jobs[j]) == 21 <= jobs[j][0] <= 10^9(required skill)1 <= jobs[j][1] <= 10^9(profit)- Multiple team members may have the same skill level
- Multiple jobs may require the same skill level
- The maximum possible profit sum can exceed
2^31 - 1but stays below2^53
Examples
Example 1
Input: max_task_profit([2, 2, 5], [[2, 30], [2, 50], [2, 10], [5, 20], [7, 60]])
Output: 160
Example 2
Input: max_task_profit([4], [[1, 9]])
Output: 9
Example 3
Input: max_task_profit([3, 3, 3], [[3, 5]])
Output: 5
Hints
Hint 1
Jobs with different skill requirements form independent subproblems since only exact matches work.
Hint 2
Within each skill group, if you have k available team members, which k jobs should they complete?
Hint 3
The contractor adds exactly one extra slot to whichever skill group benefits most from it.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The exact skill matching constraint means jobs naturally partition into independent groups by required skill level. A team member with skill s can only help with jobs requiring skill s, so different skill levels never compete for the same worker.
Within each skill group:
- If we have
kteam members available and multiple jobs, we should greedily select thekmost profitable jobs - If we have more team members than jobs, all jobs get completed
The contractor provides exactly one additional assignment slot. We can place this slot in any skill group to complete one extra job. The optimal choice is the group where the best uncompleted job has the highest profit.
Algorithm:
- Count how many team members have each skill level
- Group jobs by their required skill level
- For each skill group:
- If team members ≥ jobs: complete all jobs, add all profits to base
- Otherwise: sort jobs by profit (descending), complete the top
kjobs, track the best remaining job
- Add the best remaining job across all groups (the contractor takes it)
Correctness
Why greedy works within a group: All team members with skill s are interchangeable, and all jobs requiring skill s are equally eligible. If we have k slots and take any job with profit p while leaving a job with profit q > p, we can swap them to strictly increase the total. Therefore, the top k profits form the optimal set.
Why the contractor placement is optimal: The contractor increases exactly one group's capacity by 1. Adding it to group r lets that group complete its (k+1)-th best job, gaining that job's profit. Choosing the group with the maximum such gain is optimal.
Edge cases handled:
- Skills with no matching jobs: those team members remain idle (correct behavior)
- Jobs with no matching skills: form a group with 0 team members; only the contractor can complete one
- Fully covered groups: no remaining jobs for the contractor in that group
Complexity
Time: O(W + T log T)
- Counting team members: O(W)
- Grouping jobs: O(T)
- Sorting happens only for groups where jobs exceed team members, bounded by O(T log T) total
Space: O(W + T) for the hash maps storing counts and groups
from typing import List
def max_task_profit(team_skills: List[int], jobs: List[List[int]]) -> int:
# Count available team members at each skill level
skill_count = {}
for skill in team_skills:
skill_count[skill] = skill_count.get(skill, 0) + 1
# Group jobs by required skill
skill_groups = {}
for required_skill, profit in jobs:
skill_groups.setdefault(required_skill, []).append(profit)
base_profit = 0
best_contractor_gain = 0
for required_skill, profits in skill_groups.items():
available = skill_count.get(required_skill, 0)
if available >= len(profits):
# Enough team members to complete all jobs in this group
base_profit += sum(profits)
continue
profits.sort(reverse=True)
base_profit += sum(profits[:available])
# The contractor could take the next best uncompleted job
if profits[available] > best_contractor_gain:
best_contractor_gain = profits[available]
return base_profit + best_contractor_gain