题目如下:

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. 深度学习入门者的Python快速教程 - 基础篇

      5.1 Python简介 本章将介绍Python的最基本语法,以及一些和深度学习还有计算机视觉最相关的基本使用. 5.1.1 Python简史 Python是一门解释型的高级编程语言,特点是简单明 ...

  2. linux建立ftp用户

    #!/bin/bash sleep 1 mkdir -p /ceshi/ userdel ceshi useradd -d /ceshi -s /sbin/nologin ceshi echo mim ...

  3. numpy数组的索引和切片

    numpy数组的索引和切片 基本切片操作 >>> import numpy as np >>> arr=np.arange(10) >>> arr ...

  4. 洛谷 P1508 Likecloud 题解

    题面 很简单的一个二维DP f[i][j]表示最后吃到(i,j)所能获得的最大值, 那么f[i][j]=max(f[i+1][j-1],f[i+1][j],f[i+1][j+1])+a[i][j]; ...

  5. paramiko : 错误集整理

    错误1    时间2017-12-19:No handlers could be found for logger "paramiko.transport" 解决办法:parami ...

  6. limux密钥对配置登陆主机

    1. Linux主机免密码使用密钥登陆 这里假设主机A(192.168.0.113)用来远程连接主机B(192.168.0.186) 在主机A上执行如下命令来生成配对密钥:ssh-keygen -t ...

  7. node(koa2)跨域与获取cookie

    欲做一个node 的网关服务,通过 cookie 做信息传递,选择框架 koa2,这里简单记录跨域处理以及 cookie 获取. 首先:解决跨域问题,使用 koa2-cros 来处理,跨域问题后端处理 ...

  8. springboot项目抓数据后优化配置及四个补充

    昨天搞了一个抓取某某平台信息的抓取功能,其中有一个地址url,昨天是写死的,之前也进行配置过,印象有些模糊,今天想配置一下,在properties文件中,由此引发了下面的一系列总结操作: 1.原始模式 ...

  9. 使用layer弹窗和layui表单做新增功能

    注释:代码参考http://blog.51cto.com/825272560/1891158,在其修改之上而来,在此感谢! 1.需求:使用layer在弹窗内完成新增,成功后提示并刷新页面(父页面,li ...

  10. Linux设置静态IP后出现的几种问题

    一.设置静态IP后无法重启网卡 如下图所示 原因分析:control process exited with error code.控制进程存在错误代码. 解决方案:可以检查网卡配置文件是否修改错误. ...