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
  • 相关阅读:
    计算机
    产品经理
    Vue router-link路由不同的写法,不一样的效果
    elementui样式引入方法
    格式化启动盘win10
    删除mongdb中的数据库
    google浏览器快速清除历史记录
    js动画之弹球打砖块小游戏
    js动画之面向对象二
    js动画之面向对象一
  • 原文地址:https://www.cnblogs.com/seyjs/p/11526018.html
Copyright © 2011-2022 走看看