forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1125.cpp
46 lines (44 loc) · 1.61 KB
/
1125.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
class Solution
{
public:
vector<int> smallestSufficientTeam(vector<string>& req_skills, vector<vector<string>>& people)
{
unordered_map<string, int> key;
for (int i = 0; i < req_skills.size(); ++i) key[req_skills[i]] = i;
vector<int> people_skill(people.size(), 0);
vector<vector<int>> skill_people(req_skills.size());
for (int i = 0; i < people.size(); ++i)
{
for (string skill : people[i])
{
if (key.count(skill))
{
people_skill[i] |= 1 << key[skill];
skill_people[key[skill]].push_back(i);
}
}
}
return dfs((1 << req_skills.size()) - 1, people_skill, skill_people);
}
unordered_map<int, vector<int>> smallest_team = {{0, {}}};
vector<int> dfs(int skills_wanted, vector<int>& people_skill, vector<vector<int>>& skill_people)
{
if (smallest_team.count(skills_wanted)) return smallest_team[skills_wanted];
int smallest_team_size = INT_MAX;
vector<int> res;
int target_skill = int(log2(skills_wanted & -skills_wanted));
for (int p : skill_people[target_skill])
{
int cand_skills_wanted = skills_wanted & ~people_skill[p];
auto pre = dfs(cand_skills_wanted, people_skill, skill_people);
pre.push_back(p);
if (pre.size() < smallest_team_size)
{
smallest_team_size = pre.size();
res = pre;
}
}
smallest_team[skills_wanted] = res;
return res;
}
};