当前位置:网站首页>leetcode:1125. 最小的必要团队【状压dp板子 + 集合覆盖板子】
leetcode:1125. 最小的必要团队【状压dp板子 + 集合覆盖板子】
2022-07-21 00:21:00 【白速龙王的回眸】
分析
有一个集合
每个人其中一些点,选最少的人覆盖全集
一个点视作一位,考虑状压
dp[state]表示state的最少人选选法
一开始都是全选人,dp[0]是空
遍历每个人,看到每个人能覆盖的点
然后对于每一个state and v,如果加上这个人的点变成new_state
并且new_state大于state的话
如果new_state对应的人数要大于 v对应的人数+1
就可以更新
因为此时找到一个更小的方案
ac code
class Solution:
def smallestSufficientTeam(self, req_skills: List[str], people: List[List[str]]) -> List[int]:
# 集合覆盖问题
# 状压dp问题
# dp[state]表示state的最少人选选法
n, m = len(req_skills), len(people)
skill2index = {
skill:i for i, skill in enumerate(req_skills)}
dp = [list(range(m)) for _ in range(1 << n)] # 全选肯定可以达到要求
dp[0] = [] # init
# dp transfer
for i, skills in enumerate(people):
now_skills = 0 # 这个人的综合skills
for skill in skills:
now_skills |= 1 << skill2index[skill]
#print(bin(now_skills))
# every dp
for state in range(1 << n):
v = dp[state]
new_state = state | now_skills # 加上这个人
# 如果总技能变多,且该技能对应的人变少
if new_state > state and len(dp[new_state]) > len(v) + 1:
#print(bin(new_state), v + [i])
dp[new_state] = v + [i]
#print(dp)
return dp[(1 << n) - 1]
总结
状压dp板子 + 集合覆盖板子
边栏推荐
- 冒號用法 視頻41 13.3. 目標檢測和邊界框 QA 13.4錨框
- UE4春蓝图-生存开发游戏
- XML parsing
- C (37) FileStream
- The code called by fishmeal to xiaoyuniu. Let's see how ros2 performs point cloud PCL processing (subscription, conversion, saving)
- The design of off-site multi activity deployment of King glory mall
- 力扣133题:只出现一次的数字
- VIM editor learning notes
- ICML 2022 | 教程效度,可靠性和意义:可复现机器学习的统计方法教程
- Practical guidance of interface automation testing (Part 2): idea of setting assertion of interface automation testing
猜你喜欢
Talk about pseudo sharing
企业和个人选择云桌面的云终端还是云服务器?看完本文就知道
Design of online blog system
Talk about a wave of moveit2
02_ue4进阶_HP条和扣血机制
Vite 配置 cdn 加载资源
Practical guidance of interface automation testing (Part 2): idea of setting assertion of interface automation testing
Codeblocks的安装与配置
无码时代,企业数字化转型该如何发展?
Homeland defense | technical guide for whole stack IOT irrigation system
随机推荐
C语言编译
树莓派3B搭建Flink集群
The use method of streamwriter of C (38) and the difference between streamwriter and FileStream class
471-82(647、5、92、143、148、19)
Q#入门教程一(Q#环境配置)
Talk about 7 magic skills of redis memory optimization
Meaning of downstream task
C language compilation
C (XXXV) drawing in scrolling window
Sycl learning notes
STM32F103 key control LED program
@The difference between document annotation and Lombok
Prevent duplicate insert data in SQLite
Ros2 introductory tutorial | action communication and user-defined interface
In the code free era, how should enterprises' digital transformation develop?
[BJDCTF2020]EasySearch-1
UE4春蓝图-生存开发游戏
动态内存管理
冒號用法 視頻41 13.3. 目標檢測和邊界框 QA 13.4錨框
212. 单词搜索 II