1 Star 0 Fork 0

yuhang2__2/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
smallest-sufficient-team.cpp 1.25 KB
一键复制 编辑 原始数据 按行查看 历史
// Time: O(m * 2^n), n is the number of skills
// m is the number of people
// Space: O(2^n)
class Solution {
public:
vector<int> smallestSufficientTeam(vector<string>& req_skills, vector<vector<string>>& people) {
unordered_map<string, int> lookup;
for (int i = 0; i < req_skills.size(); ++i) {
lookup[req_skills[i]] = i;
}
unordered_map<int, vector<int>> dp;
dp[0];
for (int i = 0; i < people.size(); ++ i) {
int his_skill_set = 0;
for (const auto& skill : people[i]) {
if (lookup.count(skill)) {
his_skill_set |= 1 << lookup[skill];
}
}
auto tmp(dp);
for (const auto& [skill_set, people] : tmp) {
const auto with_him_set = skill_set | his_skill_set;
if (with_him_set == skill_set) {
continue;
}
if (!dp.count(with_him_set) ||
dp[with_him_set].size() > people.size() + 1) {
dp[with_him_set] = move(people);
dp[with_him_set].emplace_back(i);
}
}
}
return dp[(1 << req_skills.size()) - 1];
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yuhang2__2/LeetCode-Solutions.git
git@gitee.com:yuhang2__2/LeetCode-Solutions.git
yuhang2__2
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助