KatInterview

Task Assignment with Skill Matching and One Flexible Worker

Greedy & IntervalsAuto-graded · 24 test cases
MediumAuto-gradedGreedy & Intervalsgreedyhash-tablesorting

Last 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 where team_skills[i] is the skill level of team member i
  • jobs: a list where jobs[j] = [required_skill, profit] describes job j

Output:

  • A single integer representing the maximum total profit

Rules:

  • A team member with skill s can only be assigned a job with required_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^5
  • 1 <= team_skills[i] <= 10^9
  • 1 <= len(jobs) <= 10^5
  • len(jobs[j]) == 2
  • 1 <= 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 - 1 but stays below 2^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.