Skip to content

Commit 681aaa0

Browse files
authored
Create smallest-sufficient-team.cpp
1 parent 57a3c12 commit 681aaa0

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

C++/smallest-sufficient-team.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Time: O(m * 2^n), n is the number of skills
2+
// m is the number of people
3+
// Space: O(2^n)
4+
5+
class Solution {
6+
public:
7+
vector<int> smallestSufficientTeam(vector<string>& req_skills, vector<vector<string>>& people) {
8+
unordered_map<string, int> lookup;
9+
for (int i = 0; i < req_skills.size(); ++i) {
10+
lookup[req_skills[i]] = i;
11+
}
12+
unordered_map<int, vector<int>> dp;
13+
dp[0];
14+
for (int i = 0; i < people.size(); ++ i) {
15+
int his_skill_set = 0;
16+
for (const auto& skill : people[i]) {
17+
if (lookup.count(skill)) {
18+
his_skill_set |= 1 << lookup[skill];
19+
}
20+
}
21+
auto tmp(dp);
22+
for (const auto& [skill_set, people] : tmp) {
23+
const auto with_him_set = skill_set | his_skill_set;
24+
if (with_him_set == skill_set) {
25+
continue;
26+
}
27+
if (!dp.count(with_him_set) ||
28+
dp[with_him_set].size() > people.size() + 1) {
29+
dp[with_him_set] = move(people);
30+
dp[with_him_set].emplace_back(i);
31+
}
32+
}
33+
}
34+
return dp[(1 << req_skills.size()) - 1];
35+
}
36+
};

0 commit comments

Comments
 (0)