-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathSmallest Sufficient Team.cpp
73 lines (62 loc) · 2.13 KB
/
Smallest Sufficient Team.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Runtime: 10 ms (Top 99.69%) | Memory: 9.90 MB (Top 96.62%)
class Solution {
public:
std::vector<int> smallestSufficientTeam(std::vector<std::string>& req_skills, std::vector<std::vector<std::string>>& people) {
int n = req_skills.size();
int m = people.size();
std::unordered_map<std::string, int> skillToIndex;
for (int i = 0; i < n; i++) {
skillToIndex[req_skills[i]] = i;
}
std::vector<int> arr(m, 0);
for (int i = 0; i < m; i++) {
std::vector<std::string>& personSkills = people[i];
int val = 0;
for (const std::string& skill : personSkills) {
val |= 1 << skillToIndex[skill];
}
arr[i] = val;
}
std::vector<bool> banned(m, false);
for (int i = 0; i < m; i++) {
for (int j = i + 1; j < m; j++) {
int val = arr[i] | arr[j];
if (val == arr[i]) {
banned[j] = true;
}
else if (val == arr[j]) {
banned[i] = true;
}
}
}
std::vector<int> ans;
helper(0, n, arr, std::vector<int>(), banned, ans);
return ans;
}
private:
void helper(int cur, int n, const std::vector<int>& arr, std::vector<int> team, const std::vector<bool>& banned, std::vector<int>& ans) {
if (cur == (1 << n) - 1) {
if (ans.empty() || team.size() < ans.size()) {
ans = team;
}
return;
}
if (!ans.empty() && team.size() >= ans.size()) {
return;
}
int zero = 0;
while (((cur >> zero) & 1) == 1) {
zero++;
}
for (int i = 0; i < arr.size(); i++) {
if (banned[i]) {
continue;
}
if (((arr[i] >> zero) & 1) == 1) {
team.push_back(i);
helper(cur | arr[i], n, arr, team, banned, ans);
team.pop_back();
}
}
}
};