Smallest Sufficient Team Problem
Description
LeetCode Problem 1125.
In a project, you have a list of required skills req_skills, and a list of people. The i^th person people[i] contains a list of skills that the person has.
Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person.
- For example,
team = [0, 1, 3]represents the people with skillspeople[0],people[1], andpeople[3].
Return any sufficient team of the smallest possible size, represented by the index of each person. You may return the answer in any order.
It is guaranteed an answer exists.
Example 1:
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
Output: [0,2]
Example 2:
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
Output: [1,2]
Constraints:
1 <= req_skills.length <= 161 <= req_skills[i].length <= 16req_skills[i]consists of lowercase English letters.- All the strings of
req_skillsare unique. 1 <= people.length <= 600 <= people[i].length <= 161 <= people[i][j].length <= 16people[i][j]consists of lowercase English letters.- All the strings of
people[i]are unique. - Every skill in
people[i]is a skill inreq_skills. - It is guaranteed a sufficient team exists.
Solution
Approach: DP over Skill Sets
There can be 60 people, far too many to try every subset, but at most 16 skills, so every set of skills fits in a 16-bit mask and there are only 216 of them. Map each skill to a bit and turn each person into the mask of their skills.
Let size[s] be the smallest number of people whose skills together are exactly the set s, with size[0] = 0 and everything else unreachable at first. Adding person j to a team covering s gives a team covering s | mask[j], so:
- Go through the sets
sin increasing order and skip the ones not reached yet. - For each person
j, ifsize[s] + 1 < size[s | mask[j]], improve it and remember how we got there:last[s | mask[j]] = jandprev[s | mask[j]] = s.
Increasing order is safe because s | mask[j] is never smaller than s: by the time we expand a set, every way to reach it has already been tried.
The answer is the team for the full mask. Rebuild it by following the links from the full mask: take last[s], move to prev[s], and stop at the empty set. A person with no skills never improves anything, so they are never chosen.
Complexity: O(2m · n) time and O(2m) extra space, where m is the number of skills and n the number of people.
class Solution:
def smallestSufficientTeam(self, req_skills: List[str], people: List[List[str]]) -> List[int]:
index = {s: i for i, s in enumerate(req_skills)}
m, n = len(req_skills), len(people)
masks = [0] * n # masks[j] = the skills of person j, one bit per skill
for j, skills in enumerate(people):
for s in skills:
masks[j] |= 1 << index[s]
# size[s] = smallest team covering exactly the skill set s; last/prev rebuild that team.
size = [n + 1] * (1 << m)
last = [0] * (1 << m)
prev = [0] * (1 << m)
size[0] = 0
for have in range(1 << m): # a person only adds bits, so smaller sets come first
if size[have] > n:
continue # not reachable
for j in range(n):
covered = have | masks[j]
if size[have] + 1 < size[covered]:
size[covered] = size[have] + 1
last[covered] = j
prev[covered] = have
team = []
have = (1 << m) - 1
while have: # walk back from "all skills" to the empty set
team.append(last[have])
have = prev[have]
return team