【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] ...
随机推荐
- gin框架教程一: go框架gin的基本使用
我们在用http的时候一般都会用一些web框架来进行开发,gin就是这样的一个框架,它有哪些特点呢 一:gin特点 1.性能优秀2.基于官方的net/http的有限封装3.方便 灵活的中间件4.数据绑 ...
- 《Python编程从0到1》笔记5——图解递归你肯定看完就能懂!
本小节的示例比较简单,因为在每次递归过程中原问题仅缩减为单个更小的问题.这样的问题往往能够用简单循环解决.这类递归算法的函数调用图是链状结构.这种递归类型被称为“单重递归”(single recurs ...
- Sqlserver2012 sa账户登录添加其他账户
1.添加admin账户的设置 2.另外 sa登录后,点击服务器名,右键->属性->安全性 的设置: 3.其次是Sqlserver配置管理器,SQL Server服务需要重启: SQL Se ...
- HDU 1171 Big Event in HDU (动态规划、01背包)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- mysql如何下载历史版本?
进入官网 www.mysql.com
- BZOJ 1303 中位数图 题解
题面 因为所求的是中位数,所以考虑改变原序列.把大于 b 的数全部变为 1,小于 b 的数变为 −1,等于 b 则为 0.问题就变为求存在几个包含 b的区间和为 0 . 根据乘法原理,我们枚举每一个l ...
- 1.基础CRUD
在ef中,CUD都使用Datacontext.SaveChange()进行保存. SavaChange方法在保存之前会自动调用DetectChanges方法检查DataContext中做了什么更改,以 ...
- jQuery jsonp跨域请求详解
跨域的安全限制都是对浏览器端来说的,服务器端是不存在跨域安全限制的. 浏览器的同源策略限制从一个源加载的文档或脚本与来自另一个源的资源进行交互. 如果协议,端口和主机对于两个页面是相同的,则两个页面具 ...
- 扇形导航 css3
本篇文章将通过对css3中的2d变化以及过渡进行分析设计 先放上最终效果图 功能实现:1.给扇形home元素设置点击事件并添加2d旋转 2.给导航栏设置2d旋转 并通过 ...
- python 四舍五入进位不准
python中四舍五入进位不准,自己写了个方法结果: def new_round(_float, _len): ''' 四舍五入保留小数位,如果想实现 0.2 显示为 0.20,可使用 '%.2f' ...