{"id":5939,"date":"2019-12-08T02:00:16","date_gmt":"2019-12-08T10:00:16","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5939"},"modified":"2019-12-08T02:00:51","modified_gmt":"2019-12-08T10:00:51","slug":"leetcode-1282-group-the-people-given-the-group-size-they-belong-to","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1282-group-the-people-given-the-group-size-they-belong-to\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1282. Group the People Given the Group Size They Belong To"},"content":{"rendered":"\n<p>There are&nbsp;<code>n<\/code>&nbsp;people whose&nbsp;<strong>IDs<\/strong>&nbsp;go from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>n - 1<\/code>&nbsp;and each person belongs&nbsp;<strong>exactly<\/strong>&nbsp;to one&nbsp;group. Given the array&nbsp;<code>groupSizes<\/code>&nbsp;of length&nbsp;<code>n<\/code>&nbsp;telling the group size each person belongs to, return the groups there are and the people&#8217;s&nbsp;<strong>IDs<\/strong>&nbsp;each group includes.<\/p>\n\n\n\n<p>You can return any solution in any order and the same applies for IDs. Also, it is guaranteed that there exists at least one solution.&nbsp;<\/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> groupSizes = [3,3,3,3,3,1,3]\n<strong>Output:<\/strong> [[5],[0,1,2],[3,4,6]]\n<strong>Explanation:<\/strong> \nOther possible solutions are [[2,1,6],[5],[0,4,3]] and [[5],[0,6,2],[4,3,1]].\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> groupSizes = [2,1,3,3,3,2]\n<strong>Output:<\/strong> [[1],[0,5],[2,3,4]]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>groupSizes.length == n<\/code><\/li><li><code>1 &lt;= n&nbsp;&lt;= 500<\/code><\/li><li><code>1 &lt;=&nbsp;groupSizes[i] &lt;= n<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: HashMap + Greedy<\/strong><\/h2>\n\n\n\n<p>hashmap: group_size -&gt; {ids}<br>greedy: whenever a group of size s has s people, assign those s people to the same group.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<vector<int>> groupThePeople(vector<int>& groupSizes) {\n    vector<vector<int>> ans;\n    const int n =  groupSizes.size();\n    unordered_map<int, vector<int>> m;\n    for (int i = 0; i < n; ++i) {\n      auto&#038; v = m[groupSizes[i]];\n      v.push_back(i);\n      if (v.size() == groupSizes[i])\n        ans.push_back(std::move(v));                       \n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are&nbsp;n&nbsp;people whose&nbsp;IDs&nbsp;go from&nbsp;0&nbsp;to&nbsp;n &#8211; 1&nbsp;and each person belongs&nbsp;exactly&nbsp;to one&nbsp;group. Given the array&nbsp;groupSizes&nbsp;of length&nbsp;n&nbsp;telling the group size each person belongs to, return the groups there&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[88,306,82,177],"class_list":["post-5939","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-greedy","tag-group","tag-hashtable","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5939","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=5939"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5939\/revisions"}],"predecessor-version":[{"id":5941,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5939\/revisions\/5941"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5939"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}