Skip to content

Commit 1301f29

Browse files
authored
Create 1125-smallest-sufficient-team.js
1 parent 1787fa8 commit 1301f29

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

1125-smallest-sufficient-team.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @param {string[]} req_skills
3+
* @param {string[][]} people
4+
* @return {number[]}
5+
*/
6+
const smallestSufficientTeam = function(req_skills, people) {
7+
const m = req_skills.length
8+
const n = people.length
9+
const skill2Bitmap = req_skills
10+
.map((x, i) => [x, i])
11+
.reduce((dict, cur) => {
12+
dict[cur[0]] = 1 << cur[1]
13+
return dict
14+
}, {})
15+
const newPeople = people.map(x => {
16+
return x.reduce((acc, cur) => {
17+
const y = skill2Bitmap[cur]
18+
if (y !== undefined) {
19+
acc |= y
20+
}
21+
return acc
22+
}, 0)
23+
})
24+
25+
const all = (1 << m) - 1
26+
const dp = {}
27+
for (let j = 0; j < n; j++) {
28+
if (newPeople[j] > 0) {
29+
dp[newPeople[j]] = new Set([j])
30+
}
31+
}
32+
if (dp[all]) {
33+
return Array.from(dp[all]).sort()
34+
}
35+
36+
for (let k = 0; k < n; k++) {
37+
for (let s in dp) {
38+
for (let j = 0; j < n; j++) {
39+
if (newPeople[j] === 0 || dp[s].has(j)) continue
40+
const newIdx = s | newPeople[j]
41+
if (dp[newIdx] === undefined) {
42+
dp[newIdx] = new Set([...dp[s], j])
43+
if (newIdx === all) {
44+
return Array.from(dp[all]).sort()
45+
}
46+
}
47+
}
48+
}
49+
}
50+
return []
51+
}

0 commit comments

Comments
 (0)