-
Notifications
You must be signed in to change notification settings - Fork 12
/
1125. Smallest Sufficient Team
55 lines (47 loc) · 2.04 KB
/
1125. Smallest Sufficient Team
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Solution {
public:
int r; // Number of required skills
int n; // Number of people
vector<int> peoMask; // Bitmasks representing skills of each person
vector<long long> dp; // Memoization table
// Recursive function to find the minimum team
long long team(int skills_had) {
if (skills_had == 0) return 0; // Base case: no skills required
if (dp[skills_had] != INT_MAX) return dp[skills_had]; // Return memoized result if available
long long ans = n;
for (int i = 0; i < n; i++) {
int skills = skills_had & ~peoMask[i]; // Remove skills already covered by person i
if (skills != skills_had) {
long long chosen = team(skills) | 1LL << i; // Recursively find the minimum team without person i
bitset<60> bChosen(chosen);
// Check if the newly chosen team has fewer people
if (bChosen.count() < bitset<60>(dp[skills_had]).count())
dp[skills_had] = chosen; // Update the memoization table with the new minimum team
}
}
return dp[skills_had];
}
vector<int> smallestSufficientTeam(vector<string>& req_skills, vector<vector<string>>& people)
{
r = req_skills.size();
n = people.size();
// Transform skills from strings to numbers
unordered_map<string, int> skill_nr;
for (int i = 0; i < r; i++)
skill_nr[req_skills[i]] = i;
peoMask.assign(n, 0);
for (int i = 0; i < n; i++) {
for (string& skill : people[i]) {
peoMask[i] |= (1 << skill_nr[skill]); // Set the corresponding bit for each skill of person i
}
}
// DP
dp.assign(1<<r, INT_MAX);
long long peoChosen = team((1<<r)-1); // Find the minimum team to cover all required skills
bitset<60> X(peoChosen);
vector<int> ans;
for (int i = 0; i < n; i++)
if (X[i]) ans.push_back(i); // Add the selected people to the answer
return ans;
}
};