Skip to content

Commit f33aba9

Browse files
committedApr 4, 2025
Add solution #1125
1 parent dc11bd3 commit f33aba9

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,142 LeetCode solutions in JavaScript
1+
# 1,143 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -888,6 +888,7 @@
888888
1122|[Relative Sort Array](./solutions/1122-relative-sort-array.js)|Easy|
889889
1123|[Lowest Common Ancestor of Deepest Leaves](./solutions/1123-lowest-common-ancestor-of-deepest-leaves.js)|Medium|
890890
1124|[Longest Well-Performing Interval](./solutions/1124-longest-well-performing-interval.js)|Medium|
891+
1125|[Smallest Sufficient Team](./solutions/1125-smallest-sufficient-team.js)|Hard|
891892
1137|[N-th Tribonacci Number](./solutions/1137-n-th-tribonacci-number.js)|Easy|
892893
1143|[Longest Common Subsequence](./solutions/1143-longest-common-subsequence.js)|Medium|
893894
1161|[Maximum Level Sum of a Binary Tree](./solutions/1161-maximum-level-sum-of-a-binary-tree.js)|Medium|
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* 1125. Smallest Sufficient Team
3+
* https://leetcode.com/problems/smallest-sufficient-team/
4+
* Difficulty: Hard
5+
*
6+
* In a project, you have a list of required skills reqSkills, and a list of people. The ith
7+
* person people[i] contains a list of skills that the person has.
8+
*
9+
* Consider a sufficient team: a set of people such that for every required skill in reqSkills,
10+
* there is at least one person in the team who has that skill. We can represent these teams by
11+
* the index of each person.
12+
*
13+
* For example, team = [0, 1, 3] represents the people with skills people[0], people[1], and
14+
* people[3].
15+
*
16+
* Return any sufficient team of the smallest possible size, represented by the index of each
17+
* person. You may return the answer in any order.
18+
*
19+
* It is guaranteed an answer exists.
20+
*/
21+
22+
/**
23+
* @param {string[]} reqSkills
24+
* @param {string[][]} people
25+
* @return {number[]}
26+
*/
27+
var smallestSufficientTeam = function(reqSkills, people) {
28+
const skillCount = reqSkills.length;
29+
const skillMap = new Map(reqSkills.map((skill, index) => [skill, 1 << index]));
30+
const peopleSkills = people.map(skills =>
31+
skills.reduce((mask, skill) => mask | skillMap.get(skill), 0)
32+
);
33+
const target = (1 << skillCount) - 1;
34+
const dp = new Map([[0, []]]);
35+
36+
for (let i = 0; i < people.length; i++) {
37+
const currentSkill = peopleSkills[i];
38+
for (const [prevMask, team] of dp) {
39+
const newMask = prevMask | currentSkill;
40+
if (!dp.has(newMask) || dp.get(newMask).length > team.length + 1) {
41+
dp.set(newMask, [...team, i]);
42+
}
43+
}
44+
}
45+
46+
return dp.get(target);
47+
};

0 commit comments

Comments
 (0)
Please sign in to comment.