forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmallest Sufficient Team.js
40 lines (29 loc) · 1.11 KB
/
Smallest Sufficient Team.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* @param {string[]} req_skills
* @param {string[][]} people
* @return {number[]}
*/
var smallestSufficientTeam = function(req_skills, people) {
const skillIndexMap = new Map();
req_skills.forEach((skill, index) => skillIndexMap.set(skill,index) );
const skillTeamMap = new Map([[0, []]]);
people.forEach( (skills, index) => {
let hisSkills = 0;
for (const skill of skills) {
hisSkills |= 1 << skillIndexMap.get(skill);
}
for (const [currSkill, team] of skillTeamMap) {
const totalSkills = currSkill | hisSkills;
if (totalSkills === currSkill) {
continue;
}
if (
!skillTeamMap.has(totalSkills) ||
team.length + 1 < skillTeamMap.get(totalSkills).length
) {
skillTeamMap.set(totalSkills, [...team, index])
}
}
});
return skillTeamMap.get( (1<<req_skills.length) - 1);
};