当前位置: 首页 > news >正文

1125. Smallest Sufficient Team

In a project, you have a list of required skills req_skills, and a list of people. The ith person people[i] contains a list of skills that the 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]

This is a classic Shortest Path problem on a graph, but because we are dealing with a small number of required skills (up to 16), we can view it as a Dynamic Programming problem with Bitmasking.

The Logic

We represent the "set of skills" as a binary number (a bitmask).

  • If the required skills are ["java", "python", "react"], the mask 111 (7 in decimal) represents having all three.

  • The mask 010 (2 in decimal) represents having only "python".

We want to find the smallest number of people whose combined bitmasks equal the target mask (all skills).

 1 class Solution:
 2     def smallestSufficientTeam(self, req_skills: List[str], people: List[List[str]]) -> List[int]:
 3         n = len(req_skills)
 4         # Map skill name to its bit position
 5         skill_to_idx = {skill: i for i, skill in enumerate(req_skills)}
 6         target_mask = (1 << n) - 1
 7         
 8         # dp[mask] = list of indices of people who cover those skills
 9         # Initialize with the empty set for mask 0
10         dp = {0: []}
11         
12         for i, person_skills in enumerate(people):
13             # Convert current person's skills into a bitmask
14             current_person_mask = 0
15             for skill in person_skills:
16                 if skill in skill_to_idx:
17                     current_person_mask |= (1 << skill_to_idx[skill])
18             
19             # Try to combine this person with all skill sets we've found so far
20             # We iterate over a copy of keys to avoid "dictionary changed size during iteration"
21             for mask, team in list(dp.items()):
22                 new_mask = mask | current_person_mask
23                 
24                 # If we found a new skill combination OR a shorter way to get an existing one
25                 if new_mask not in dp or len(dp[new_mask]) > len(team) + 1:
26                     dp[new_mask] = team + [i]
27                     
28         return dp[target_mask]

 

http://www.jsqmd.com/news/405395/

相关文章:

  • 【Effective Modern C++】第七章 并发API:35. 优先考虑基于任务的编程而非基于线程的编程
  • oh-my-opencode 模型配置
  • 调试方法
  • 2026年 南通宠物医院推荐榜:异宠诊疗与猫科专科的暖心口碑之选 - 品牌企业推荐师(官方)
  • 户外储能电源双向逆变器电路方案:高效率DC-DC软开关,强负载适应性,智能控制与自检功能,功率...
  • python驾考驾校在线学习与测试系统(编号:98492256)
  • 横评后发现 9个一键生成论文工具:继续教育毕业论文写作必备测评与推荐
  • Python自助旅游系统 自驾游攻略系统
  • 导师严选 9个AI论文工具:本科生毕业论文写作全攻略
  • 北美的留美求职身份规划服务哪家靠谱?2026机构测评(避坑) - 品牌排行榜
  • 走进OMO模式电商零售,2026年这些平台有亮点,全流程数字化运营/OMO模式电商零售,OMO模式电商零售系统推荐榜单 - 品牌推荐师
  • 实测才敢推!9个AI论文网站测评:MBA毕业论文写作必备工具推荐
  • 第十五五规划中的AI Agent红利:新质生产力引擎与开发者实战指南(2026–2030)
  • 2026年高压管件市场盘点:新型弯头管件厂家有哪些,法兰管件/管道/高压管件/防腐管道,高压管件生产厂家有哪些 - 品牌推荐师
  • 省心了! 降AI率软件 千笔 VS WPS AI,专科生专属神器!
  • 2026年市面上正规的八边封包装袋加工厂排行榜,四边封包装袋/三边封拉链袋/自立拉链袋,八边封包装袋供货厂家哪家好 - 品牌推荐师
  • 国产强势SCI和国外传统SCI该选择哪个?
  • 2005-2025年我国逐日露点温度栅格数据
  • Python写真摄影旅拍预约管理系统
  • python员工宿舍管理系统(编号:10039121)
  • qq机器人 连接本地llm 注意这个c2c
  • python基于django框架和协同过滤算法的图书推荐系统设计与实现_7b2iz8a3
  • jspm酒店客房预定系统_65gd09g4
  • 5家抖音优化推广公司服务和优势拆解
  • python健身房管理系统(编号:27805230)
  • java基于springboot框架的APP开发者信息管理平台的设计与实现(编号:40791381)安卓
  • 开题报告总被退回?别再靠“感觉”写了!百考通AI 10分钟生成导师点赞的开题初稿
  • 真的太省时间! 降AI率网站 千笔·降AI率助手 VS 云笔AI
  • 开题卡在综述?百考通AI输入关键词,输出可直接用的综述初稿!
  • Claude Code 免费从入门到精通