题目如下:

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

【leetcode】1125. Smallest Sufficient Team的更多相关文章

  1. 【leetcode】1081. Smallest Subsequence of Distinct Characters

    题目如下: Return the lexicographically smallest subsequence of text that contains all the distinct chara ...

  2. 【LeetCode】1022. Smallest Integer Divisible by K 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 【LeetCode】988. Smallest String Starting From Leaf 解题报告(C++ & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  4. 【LeetCode】910. Smallest Range II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. 【LeetCode】632. Smallest Range 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/smallest ...

  6. 【LeetCode】908. Smallest Range I 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学计算 日期 题目地址:https://leetc ...

  7. 【leetcode】302.Smallest Rectangle Enclosing Black Pixels

    原题 An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The bl ...

  8. 【leetcode】1257. Smallest Common Region

    题目如下: You are given some lists of regions where the first region of each list includes all other reg ...

  9. 【leetcode】1202. Smallest String With Swaps

    题目如下: You are given a string s, and an array of pairs of indices in the string pairs where pairs[i] ...

随机推荐

  1. Prometheus告警模型分析

    Prometheus作为时下最为流行的开源监控系统,其庞大的生态体系:包括针对各种传统应用的Exporter,完整的二次开发工具链,与Kubernetes等主流平台的高度亲和以及由此带来的强大的自发现 ...

  2. 浏览器访问ipv6站点(未绑定主机的ipv6站点)

    我们在浏览器直接输入ipv6地址敲回车,一般情况下浏览器会跳转到搜索引擎进行搜索. 我们需要在浏览器器中输入: http://[::1]  或者 [::1]

  3. js获取当天时间,7天前后时间,时间格式化

    格式化时间年月日时分秒 //时间戳转换方法 date:时间戳数字 formatDate(date) { var date = new Date(date); var YY = date.getFull ...

  4. LeetCode.993-二叉树中的堂兄弟(Cousins in Binary Tree)

    这是悦乐书的第374次更新,第401篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第235题(顺位题号是993).在二叉树中,根节点在深度0处,并且每个深度为k的节点的子 ...

  5. 【VS开发】免费打工仔:一个完善的ActiveX Web控件教程

    作者 David Marcionek. 翻译 免费打工仔 这个教程可以帮助你快速开发一个ActiveX控件.其中将要讲解关于ActiveX开发的一些基础概念,诸如方法(method).属性(prope ...

  6. 使用UI Automation实现自动化测试--1-4

    Introduction UI Automation是Microsoft .NET 3.0框架下提供的一种用于自动化测试的技术,是在MSAA基础上建立的,MSAA就是Microsoft Active ...

  7. python 三元运算、列表推倒式、字典推倒式、生成器生成式

    1.三元运算 name=input('姓名>>: ') res='SB' if name == 'alex' else 'NB' print(res) 2.列表推倒式 #1.示例 egg_ ...

  8. k8s-kubernetes-configmap存储

    存储 configMap configMap描述信息 ConfigMap功能在Kubernetes1.2版本中引入,许多应用程序会从配置文件.命令行参数或环境变量中读取配置信息. ConfigMap ...

  9. 第六周作业&实验报告四

    一.实验目的 (1)掌握类的继承 (2)变量的继承和覆盖,方法的继承,重载和覆盖的实现: 二.实验的内容 (1)根据下面的要求实现圆类Circle. 1.圆类Circle的成员变量:radius表示圆 ...

  10. 13.56Mhz/NFC读写器天线阻抗匹配调试步骤-20191128

    相关原文: https://blog.csdn.net/wwt18811707971/article/details/80641432 http://www.52rd.com/Blog/Detail_ ...