题目如下:

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. #Java学习之路——基础阶段二(第十四篇)

    我的学习阶段是跟着CZBK黑马的双源课程,学习目标以及博客是为了审查自己的学习情况,毕竟看一遍,敲一遍,和自己归纳总结一遍有着很大的区别,在此期间我会参杂Java疯狂讲义(第四版)里面的内容. 前言: ...

  2. C语言循环队列

    #include<stdio.h> #include<stdlib.h> #include<string.h> //循环队列 typedef struct { in ...

  3. 关于Tomcat配置问题

    一,部署并启动Tomcat服务器 Tomcat: 开源的 Servlet 容器. 解压 apache-tomcat-6.0.16.zip 到一个非中文目录下 配置一个环境变量. java_home(指 ...

  4. vue--生命周期演示

    vue实例的生命周期:https://cn.vuejs.org/v2/guide/instance.html#%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA-Vue-%E5% ...

  5. python 并发编程 多线程 GIL与Lock

    GIL与Lock Python已经有一个GIL来保证同一时间只能有一个线程来执行了,为什么这里还需要互斥锁lock? 锁的目的是为了保护共享的数据,同一时间只能有一个线程来修改共享的数据 GIT保证了 ...

  6. PTA 7-20 表达式转换

    转自:https://www.cnblogs.com/yuxiaoba/p/8399934.html 算术表达式有前缀表示法.中缀表示法和后缀表示法等形式.日常使用的算术表达式是采用中缀表示法,即二元 ...

  7. Sqoop-MySQL导入hive时id为文本解决

    错误如下 // :: ERROR tool.ImportTool: Import failed: java.io.IOException: Generating splits for a textua ...

  8. 【转】mysql用sql实现split函数

    关键词:mysql split mysql根据逗号将一行数据拆分成多行数据1.原始数据演示 2.处理结果演示 3.sql语句 SELECT a.id , a.NAME , substring_inde ...

  9. Tomcat部署SSL证书过程中遇到的问题

    在CentOS7中Tomcat部署SSL证书时遇到的问题 1.配置servlet.xml 配置tomcat/conf/server.xml 加入以下代码 <Connector port=&quo ...

  10. python中对多态和多态性的理解

    python中对多态的理解 一.多态 多态是指一类事物有多种形态,比如动物类,可以有猫,狗,猪等等.(一个抽象类有多个子类,因而多态的概念依赖于继承) import abc class Animal( ...