Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Smallest Sufficient Team in Pyhton
The Smallest Sufficient Team problem asks us to find the minimum number of people needed to cover all required skills for a project. Given a list of required skills and people with their respective skills, we need to select the smallest team that collectively possesses all required skills.
Problem Understanding
A sufficient team is a set of people where every required skill is covered by at least one team member. We represent teams using indices − for example, team [0, 2] means people at positions 0 and 2 in the people array.
Algorithm Approach
We use dynamic programming with bitmasks to solve this efficiently ?
dp− maps skill combinations (as bitmasks) to the smallest team covering those skillskey− maps each required skill to a unique bit positionFor each person, calculate their skill bitmask and update all possible combinations
Return the team that covers all skills (bitmask with all bits set)
Example
Let's implement the solution step by step ?
class Solution(object):
def smallestSufficientTeam(self, req_skills, people):
# dp[skill_mask] = smallest team that covers skills in skill_mask
dp = {0: []}
# Map each skill to a bit position
skill_to_bit = {skill: i for i, skill in enumerate(req_skills)}
for person_idx, person_skills in enumerate(people):
# Calculate bitmask for current person's skills
current_skill_mask = 0
for skill in person_skills:
if skill in skill_to_bit:
current_skill_mask |= 1 << skill_to_bit[skill]
# Try adding this person to existing teams
for skill_set, team_members in list(dp.items()):
new_skill_set = skill_set | current_skill_mask
# Skip if no new skills added
if new_skill_set == skill_set:
continue
# Update if we found a smaller team for this skill combination
if new_skill_set not in dp or len(dp[new_skill_set]) > len(team_members) + 1:
dp[new_skill_set] = team_members + [person_idx]
# Return team that covers all required skills
all_skills_mask = (1 << len(req_skills)) - 1
return dp[all_skills_mask]
# Test the solution
solution = Solution()
req_skills = ["java", "flutter", "android"]
people = [["java"], ["android"], ["flutter", "android"]]
result = solution.smallestSufficientTeam(req_skills, people)
print("Required skills:", req_skills)
print("People and their skills:", people)
print("Smallest sufficient team (indices):", result)
print("Team members' skills:", [people[i] for i in result])
Required skills: ['java', 'flutter', 'android'] People and their skills: [['java'], ['android'], ['flutter', 'android']] Smallest sufficient team (indices): [0, 2] Team members' skills: [['java'], ['flutter', 'android']]
How It Works
The algorithm uses bitmasks to efficiently represent skill combinations ?
Skill Mapping: Each skill gets a unique bit position (java=0, flutter=1, android=2)
Bitmask Representation: Person with ["flutter", "android"] has mask 110 (binary) = 6 (decimal)
Dynamic Programming: For each skill combination, we store the smallest team that achieves it
Optimization: We only update when we find a smaller team for a skill combination
Example Walkthrough
| Person | Skills | Bitmask | Binary |
|---|---|---|---|
| 0 | ["java"] | 1 | 001 |
| 1 | ["android"] | 4 | 100 |
| 2 | ["flutter", "android"] | 6 | 110 |
The target is bitmask 7 (111 binary) covering all three skills. Team [0, 2] achieves this: 1 | 6 = 7.
Conclusion
The Smallest Sufficient Team problem is solved efficiently using dynamic programming with bitmasks. This approach ensures we find the minimum team size while exploring all possible skill combinations systematically.
