From cc6c4312eaa12c658387b3f1a60be2b36f6be9e1 Mon Sep 17 00:00:00 2001 From: Eatswap Date: Sat, 8 Apr 2023 14:50:32 +0800 Subject: [PATCH] add: 230408-CN --- cpp/2304/230408-CN.cpp | 88 +++++++++++++++++++++++++++++++++++++++++ cpp/2304/CMakeLists.txt | 2 +- 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 cpp/2304/230408-CN.cpp diff --git a/cpp/2304/230408-CN.cpp b/cpp/2304/230408-CN.cpp new file mode 100644 index 0000000..44aae5f --- /dev/null +++ b/cpp/2304/230408-CN.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +#include +#include +#include + +/** + * 1125. Smallest Sufficient Team + * + * In a project, you have a list of required skills req_skills, and a list of people. The ith person people[i] contains a list of skills that the person has. + * + * Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person. + * + * For example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3]. + * Return any sufficient team of the smallest possible size, represented by the index of each person. You may return the answer in any order. + * + * It is guaranteed an answer exists. + */ + +class Solution { +public: + template + using V = std::vector; + using VI = V; + using VS = V; + using VVS = V; + + static VI smallestSufficientTeam(const VS&, const VVS&); +}; + +Solution::VI Solution::smallestSufficientTeam(const VS& req, const VVS& p) { + int n = 0; + std::unordered_map m; + for (auto&& x : req) + m[x] = n++; + VI ps; + for (auto&& x : p) { + int y = 0; + for (auto&& z : x) + y |= (1 << m[z]); + ps.push_back(y); + } + VI dp(1 << 16), father(1 << 16); + const int full = (1 << n) - 1, k = ps.size(); + std::function d = [&](int current) { + if (dp[current]) + return dp[current]; + if (current == full) + return 0; + int& ans = dp[current]; + ans = 0x7FFFFFFF; + for (int i = 0, next; i < k; ++i) { + next = current | ps[i]; + if (current == next) + continue; + if (int dn = d(next); 1 + dn < ans) { + ans = 1 + dn; + father[current] = i; + } + } + return ans; + }; + d(0); + VI ret; + for (int i = 0; i < full; i = i | ps[father[i]]) + ret.push_back(father[i]); + return ret; +} + +template +std::ostream& operator<<(std::ostream& os, std::vector vec) { + for (auto&& x : vec) + os << x; + return os; +} + +int main() { + std::cout << Solution::smallestSufficientTeam({"algorithms","math","java","reactjs","csharp","aws"}, { + {"algorithms","math","java"}, + {"algorithms","math","reactjs"}, + {"java","csharp","aws"}, + {"reactjs","csharp"}, + {"csharp","math"}, + {"aws","java"} + }); + return 0; +} diff --git a/cpp/2304/CMakeLists.txt b/cpp/2304/CMakeLists.txt index 97b398f..0c2616b 100644 --- a/cpp/2304/CMakeLists.txt +++ b/cpp/2304/CMakeLists.txt @@ -4,4 +4,4 @@ PROJECT(2304) SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_EXPORT_COMPILE_COMMANDS true) -ADD_EXECUTABLE(2304 230407.cpp) +ADD_EXECUTABLE(2304 230408-CN.cpp)