【leetcode】1125. Smallest Sufficient Team
题目如下:
In a project, you have a list of required skills
req_skills, and a list ofpeople. The i-th personpeople[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 skillspeople[0],people[1], andpeople[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 <= 161 <= people.length <= 601 <= people[i].length, req_skills[i].length, people[i][j].length <= 16- Elements of
req_skillsandpeople[i]are (respectively) distinct.req_skills[i][j], people[i][j][k]are lowercase English letters.- Every skill in
people[i]is a skill inreq_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的更多相关文章
- 【leetcode】1081. Smallest Subsequence of Distinct Characters
题目如下: Return the lexicographically smallest subsequence of text that contains all the distinct chara ...
- 【LeetCode】1022. Smallest Integer Divisible by K 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】988. Smallest String Starting From Leaf 解题报告(C++ & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 【LeetCode】910. Smallest Range II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】632. Smallest Range 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/smallest ...
- 【LeetCode】908. Smallest Range I 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学计算 日期 题目地址:https://leetc ...
- 【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 ...
- 【leetcode】1257. Smallest Common Region
题目如下: You are given some lists of regions where the first region of each list includes all other reg ...
- 【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] ...
随机推荐
- LeetCode.1005-K次取负数组和最大(Maximize Sum Of Array After K Negations)
这是悦乐书的第376次更新,第403篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第237题(顺位题号是1005).给定一个整数数组A,我们必须按以下方式修改数组:我们选 ...
- 使用ocelot作为api网关
新建网站项目然后添加ocelot 的nuget包 新建ocelot.json的网关的配置文件 { "GlobalConfiguration": { "BaseUrl&qu ...
- Shell 变量详解教程之位置变量与预定义变量
Shell 变量分为3部分,分别是用户自定义变量.位置变量和预定义变量. 一. 自定义变量 那么,什么是变量呢?简单的说,就是让某一个特定字符串代表不固定的内容,用户定义的变量是最普通的Shell ...
- springboot - 应用实践(1)认识springboot
1.为什么要推出springboot springboot设计的目的是用来简化新spring应用的初始搭建以及开发过程.springboot遵循“约定优于配置”原则. 2.springboot默认的配 ...
- Maven - Maven3实战学习笔记(3)使用maven构建Web应用
1.jetty-maven-plugin自动化测试Web应用工具 <plugin> <groupId>org.mortbay.jetty</groupId> < ...
- 2019 Multi-University Training Contest 8 - 1006 - Acesrc and Travel - 树形dp
http://acm.hdu.edu.cn/showproblem.php?pid=6662 仿照 CC B - TREE 那道题的思路写的,差不多.也是要走路径. 像这两种必须走到叶子的路径感觉是必 ...
- php前台表单限制PHP上传大小
在php文件上传时候,一般我都认为考虑php.ini配置修改文件上传大小,还后台控制上传大小,这里教你php前台表单限制PHP上传大小 <form action="http://www ...
- C语言中将二维数组作为函数参数来传递
c语言中经常需要通过函数传递二维数组,有三种方法可以实现,如下: 方法一, 形参给出第二维的长度. 例如: #include <stdio.h> void func(int n, char ...
- 30分钟Maven入门到精通
Maven是近年来最流行的项目构建与管理工具.不仅简化了我们开发过程中对jar包依赖的导入,还对项目的清理.初始化.编译.测试.打包.集成测试.验证.部署和站点生成等所有构建过程进行了抽象和统一,方便 ...
- src和href都是链接有啥不一样
前言 src和href都是用于外部资源的引入,像图片.CSS文件.HTML文件.js文件或其他web页面等.那么在SRC和HREF之间是否有一个明确的区别呢?在哪些地方或者什么时候应该用SRC还是HR ...