Smallest Sufficient Team in Pyhton


Suppose for a project we have a list of required skills called req_skills, and a list of people. Here i-th people people[i] contains a list of skills that person has.

Now suppose a sufficient team is defined as 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: As an example suppose team is [0, 1, 3] this represents the people with skills people[0], people[1], and people[3].

We have to find the team of the smallest possible size.

You may return the answer in any order. It is guaranteed an answer exists.

So, if the input is like req_skills = ["java","flutter","android"], people = [["java"],["android"],["flutter","android"]], then the output will be [0,2]

To solve this, we will follow these steps −

  • dp := one map, add empty list corresponding to key 0

  • key := a map like (value,i) where value is from req_skills and i are numbers

  • for number,person pair (i, p) from the people array by taking people and assign them number −

    • current_skill := 0

    • for skill in p

      • current_skill := current_skill OR 2^key[skill]

    • for (skill_set,members) pair is in dp key-value pairs −

      • total_skill := skill_set OR current_skill

      • if total_skill is same as skill_set, then −

        • Ignore following part, skip to the next iteration

      • if total_skill is not in do or size of dp[total_skill] > size of members + 1, then

        • dp[total_skill] := members + [i]

  • return dp[(1 << len(req_skills))

Let us see the following implementation to get better understanding −

Example

class Solution(object):
   def smallestSufficientTeam(self, req_skills, people):
      dp = {0:[]}
      key = {v:i for i,v in enumerate(req_skills)}
      for i,p in enumerate(people):
         current_skill = 0
         for skill in p:
         current_skill |= 1<< key[skill]
      for skill_set, members in dp.items():
         total_skill = skill_set|current_skill
         if total_skill == skill_set:
            continue
         if total_skill not in dp or len(dp[total_skill])>
len(members)+1:
            dp[total_skill] = members + [i]
      return dp[(1<<len(req_skills)) - 1]
ob = Solution()
print(ob.smallestSufficientTeam(["java","flutter","android"],
[["java"],["android"],["flutter","android"]]))

Input

["java","flutter","android"]
[["java"],["android"],["flutter","android"]]

Output

[0,2]

Updated on: 04-Jun-2020

207 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements