【leetcode】1125. Smallest Sufficient Team

题目如下:

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 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 skills people[0]people[1], and people[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 <= 16
  • 1 <= people.length <= 60
  • 1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16
  • Elements of req_skills and people[i] are (respectively) distinct.
  • req_skills[i][j], people[i][j][k]are lowercase English letters.
  • Every skill in people[i] is a skill in req_skills.
  • It is guaranteed a sufficient team exists.

解题思路:首先要过滤掉肯定过滤掉肯定不会入选的人,这些人符合两个条件中的一个:一是没有任何技能,二是会的技能是其余人的技能的子集。接下来就是用DFS计算出最终的结果。在计算的过程中,为了提高技能计算并集的效率,可以把所有的技能都转成2^n的形式,例如 req_skills = ["java","nodejs","reactjs"],java用2^0次方表示,nodejs用2^1表示,reactjs用2^2。 每个人的技能也用同样的方式表示,例如 people = [["java"]]可以表示成 people = [[1]]。

代码如下:

class Solution(object):
    def smallestSufficientTeam(self, req_skills, people):
        """
        :type req_skills: List[str]
        :type people: List[List[str]]
        :rtype: List[int]
        """
        dic = {}
        for i in range(len(req_skills)):
            dic[req_skills[i]] = i
        plist = []
        for item_list in people:
            val = 0
            for item in item_list:
                val += 2 ** dic[item]
            plist.append(val)
        for i in range(len(plist)):
            if plist[i] == 0:
                continue
            for j in range(i+1,len(plist)):
                if plist[i] | plist[j] == plist[i]:
                    plist[j] = 0
                elif plist[i] | plist[j] == plist[j]:
                    plist[i] = 0
                    break
        res = None
        queue = []
        for i, v in enumerate(plist):
            if v != 0: queue.append(([i], v))

        while len(queue) > 0:
            vl, v = queue.pop(0)
            if v == (2 ** (len(req_skills)) - 1):
                if (res == None or len(res) > len(vl)):
                    res = vl[:]
                continue
            elif res != None and len(vl) >= len(res):
                continue
            for i in range(vl[-1] + 1, len(plist)):
                if v | plist[i] == v:
                    continue
                elif v | plist[i] == plist[i]:
                    break
                else:
                    tl = vl[:] + [i]
                    queue.insert(0,(tl, v | plist[i]))
        return res

 

转载于:https://www.cnblogs.com/seyjs/p/11526018.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值