{"id":5300,"date":"2019-07-15T20:13:57","date_gmt":"2019-07-16T03:13:57","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5300"},"modified":"2019-07-15T20:16:14","modified_gmt":"2019-07-16T03:16:14","slug":"leetcode-1125-smallest-sufficient-team","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1125-smallest-sufficient-team\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1125. Smallest Sufficient Team"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1125. Smallest Sufficient Team - \u5237\u9898\u627e\u5de5\u4f5c EP257\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/5Kr1PWAgEx8?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>In a project, you have a list of required skills&nbsp;<code>req_skills<\/code>,&nbsp;and a list of&nbsp;<code>people<\/code>.&nbsp; The i-th person&nbsp;<code>people[i]<\/code>&nbsp;contains a list of skills that person has.<\/p>\n\n\n\n<p>Consider a&nbsp;<em>sufficient team<\/em>: a set of people such that for every required skill in&nbsp;<code>req_skills<\/code>, there is at least one person in the team who has that skill.&nbsp; We can represent these teams by the index of each person: for example,&nbsp;<code>team = [0, 1, 3]<\/code>&nbsp;represents the people with skills&nbsp;<code>people[0]<\/code>,&nbsp;<code>people[1]<\/code>, and&nbsp;<code>people[3]<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<strong>any<\/strong>&nbsp;sufficient team of the smallest possible size, represented by the index of each person.<\/p>\n\n\n\n<p>You may return the answer in any order.&nbsp; It is guaranteed an answer exists.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> req_skills = [\"java\",\"nodejs\",\"reactjs\"], people = [[\"java\"],[\"nodejs\"],[\"nodejs\",\"reactjs\"]]\n<strong>Output:<\/strong> [0,2]\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> req_skills = [\"algorithms\",\"math\",\"java\",\"reactjs\",\"csharp\",\"aws\"], people = [[\"algorithms\",\"math\",\"java\"],[\"algorithms\",\"math\",\"reactjs\"],[\"java\",\"csharp\",\"aws\"],[\"reactjs\",\"csharp\"],[\"csharp\",\"math\"],[\"aws\",\"java\"]]\n<strong>Output:<\/strong> [1,2]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= req_skills.length &lt;= 16<\/code><\/li><li><code>1 &lt;= people.length &lt;= 60<\/code><\/li><li><code>1 &lt;= people[i].length, req_skills[i].length, people[i][j].length&nbsp;&lt;= 16<\/code><\/li><li>Elements of&nbsp;<code>req_skills<\/code>&nbsp;and&nbsp;<code>people[i]<\/code>&nbsp;are (respectively) distinct.<\/li><li><code>req_skills[i][j], people[i][j][k]<\/code>&nbsp;are&nbsp;lowercase English letters.<\/li><li>It is guaranteed a sufficient team exists.<\/li><\/ul>\n\n\n\n<p><strong>Solution: DP<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/07\/1125-ep257.png\" alt=\"\" class=\"wp-image-5301\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/07\/1125-ep257.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/07\/1125-ep257-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/07\/1125-ep257-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++\/Array<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua, 24 ms \/ 11 MB\nclass Solution {\npublic:\n  vector<int> smallestSufficientTeam(vector<string>& req_skills, vector<vector<string>>& people) {    \n    const int n = req_skills.size();\n    const int target = (1 << n) - 1;\n    \n    vector<int> skills;\n    for (const auto& p : people) {\n      int mask = 0;\n      for (const string& s: p)\n        mask |= (1 << find(begin(req_skills), end(req_skills), s) - begin(req_skills));\n      skills.push_back(mask);\n    }\n    \n    vector<int> dp((1 << n), INT_MAX \/ 2);\n    vector<pair<int, int>> pt((1 << n));\n    dp[0] = 0;\n    \n    for (int i = 0; i < people.size(); ++i) {\n      const int k = skills[i];\n      if (k == 0) continue;\n      for (int j = target; j >= 0; --j)            \n        if (dp[j] + 1 < dp[j | k]) {\n          dp[j | k] = dp[j] + 1;\n          pt[j | k] = {j, i};   \n        }\n    }\n    \n    int t = target;\n    vector<int> ans;\n    while (t) {\n      ans.push_back(pt[t].second);\n      t = pt[t].first;\n    }\n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">C++\/HashTable<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua, 272 ms \/ 94.8 MB\nclass Solution {\npublic:\n  vector<int> smallestSufficientTeam(vector<string>& req_skills, vector<vector<string>>& people) {    \n    const int n = req_skills.size();    \n    \n    vector<int> skills;\n    for (const auto& p : people) {\n      int mask = 0;\n      for (const string& s: p)\n        mask |= (1 << find(begin(req_skills), end(req_skills), s) - begin(req_skills));\n      skills.push_back(mask);\n    }\n    \n    unordered_map<int, vector<int>> dp;\n    dp[0] = {};\n            \n    for (int i = 0; i < people.size(); ++i) {      \n      unordered_map<int, vector<int>> tmp(dp);\n      for (const auto& kv : dp) {\n        int k = kv.first | skills[i];\n        if (!tmp.count(k) || kv.second.size() + 1 < tmp[k].size()) {\n          tmp[k] = kv.second;\n          tmp[k].push_back(i);\n        }        \n      }\n      tmp.swap(dp);\n    }    \n      \n    return dp[(1 << n) - 1];\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In a project, you have a list of required skills&nbsp;req_skills,&nbsp;and a list of&nbsp;people.&nbsp; The i-th person&nbsp;people[i]&nbsp;contains a list of skills that person has. Consider a&nbsp;sufficient&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[16,18,217],"class_list":["post-5300","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-bit","tag-dp","tag-hard","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5300","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5300"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5300\/revisions"}],"predecessor-version":[{"id":5305,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5300\/revisions\/5305"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}