zoukankan      html  css  js  c++  java
  • 【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
  • 相关阅读:
    为应用程序的选项卡及ActionBar设置样式
    在Flex中定义移动设备应用程序和启动屏幕
    在Flex中用于处理XML对象的E4X 方法
    在移动设备应用程序中嵌入字体
    【2020-01-14】一些不起眼的常态局限
    【2020-01-13】一点一点的,一点一点的
    【2020-01-12】昨天跟老骨干聚了个会
    【2020-01-11】多警惕自己的自圆其说
    【2020-01-10】每天早上一个小时的自我修养
    【2020-01-09】看书就是一种很好的娱乐
  • 原文地址:https://www.cnblogs.com/seyjs/p/11526018.html
Copyright © 2011-2022 走看看