{"id":8306,"date":"2021-04-05T00:34:44","date_gmt":"2021-04-05T07:34:44","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8306"},"modified":"2021-04-05T19:00:39","modified_gmt":"2021-04-06T02:00:39","slug":"leetcode-1815-maximum-number-of-groups-getting-fresh-donuts","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1815-maximum-number-of-groups-getting-fresh-donuts\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1815. Maximum Number of Groups Getting Fresh Donuts"},"content":{"rendered":"\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1815. Maximum Number of Groups Getting Fresh Donuts - \u5237\u9898\u627e\u5de5\u4f5c EP391\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/ZpFno2IEHWE?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>There is a donuts shop that bakes donuts in batches of&nbsp;<code>batchSize<\/code>. They have a rule where they must serve&nbsp;<strong>all<\/strong>&nbsp;of the donuts of a batch before serving any donuts of the next batch. You are given an integer&nbsp;<code>batchSize<\/code>&nbsp;and an integer array&nbsp;<code>groups<\/code>, where&nbsp;<code>groups[i]<\/code>&nbsp;denotes that there is a group of&nbsp;<code>groups[i]<\/code>&nbsp;customers that will visit the shop. Each customer will get exactly one donut.<\/p>\n\n\n\n<p>When a group visits the shop, all customers of the group must be served before serving any of the following groups. A group will be happy if they all get fresh donuts. That is, the first customer of the group does not receive a donut that was left over from the previous group.<\/p>\n\n\n\n<p>You can freely rearrange the ordering of the groups. Return&nbsp;<em>the&nbsp;<strong>maximum<\/strong>&nbsp;possible number of happy groups after rearranging the groups.<\/em><\/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> batchSize = 3, groups = [1,2,3,4,5,6]\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> You can arrange the groups as [6,2,4,5,1,3]. Then the 1<sup>st<\/sup>, 2<sup>nd<\/sup>, 4<sup>th<\/sup>, and 6<sup>th<\/sup> groups will be happy.\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> batchSize = 4, groups = [1,3,2,5,2,2,1,6]\n<strong>Output:<\/strong> 4\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= batchSize &lt;= 9<\/code><\/li><li><code>1 &lt;= groups.length &lt;= 30<\/code><\/li><li><code>1 &lt;= groups[i] &lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1815-ep391-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1815-ep391-1.png\" alt=\"\" class=\"wp-image-8314\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1815-ep391-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1815-ep391-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1815-ep391-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 0: Binary Mask DP<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1815-ep391-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1815-ep391-2.png\" alt=\"\" class=\"wp-image-8315\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1815-ep391-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1815-ep391-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1815-ep391-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<p>Time complexity: O(n*2<sup>n<\/sup>) TLE<br>Space complexity: O(2<sup>n<\/sup>)<\/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, TLE, 42\/67 Passed\nclass Solution {\npublic:\n  int maxHappyGroups(int b, vector<int>& groups) {\n    const int n = groups.size();\n    vector<int> dp(1 << n);\n    for (int mask = 0; mask < 1 << n; ++mask) {\n      int s = 0;\n      for (int i = 0; i < n; ++i)\n        if (mask &#038; (1 << i)) s = (s + groups[i]) % b;\n      for (int i = 0; i < n; ++i)\n        if (!(mask &#038; (1 << i)))\n          dp[mask | (1 << i)] = max(dp[mask | (1 << i)],\n                                    dp[mask] + (s == 0));\n    }\n    return dp[(1 << n) - 1];\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Recursion w\/ Memoization<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1815-ep391-3.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1815-ep391-3.png\" alt=\"\" class=\"wp-image-8316\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1815-ep391-3.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1815-ep391-3-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/04\/1815-ep391-3-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<p>State: count of group size % batchSize<\/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, 888 ms, 61.9 MB\nclass Solution {\npublic:\n  int maxHappyGroups(int b, vector<int>& groups) {\n    int ans = 0;\n    vector<int> count(b);\n    for (int g : groups) ++count[g % b];      \n    map<vector<int>, int> cache;\n    function<int(int)> dp = [&](int s) {\n      auto it = cache.find(count);\n      if (it != cache.end()) return it->second;\n      int ans = 0;\n      for (int i = 1; i < b; ++i) {\n        if (!count[i]) continue;\n        --count[i];\n        ans = max(ans, (s == 0) + dp((s + i) % b));\n        ++count[i];\n      }\n      return cache[count] = ans;\n    };    \n    return count[0] + dp(0);\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 380 ms, 59.2 MB\nstruct VectorHasher {\n  size_t operator()(const vector<int>& V) const {\n    size_t hash = V.size();\n    for (auto i : V)\n      hash ^= i + 0x9e3779b9 + (hash << 6) + (hash >> 2);\n    return hash;\n  }\n};\nclass Solution {\npublic:\n  int maxHappyGroups(int b, vector<int>& groups) {\n    int ans = 0;\n    vector<int> count(b);\n    for (int g : groups) ++count[g % b];      \n    unordered_map<vector<int>, int, VectorHasher> cache;\n    function<int(int)> dp = [&](int s) {\n      auto it = cache.find(count);\n      if (it != cache.end()) return it->second;\n      int ans = 0;\n      for (int i = 1; i < b; ++i) {\n        if (!count[i]) continue;\n        --count[i];\n        ans = max(ans, (s == 0) + dp((s + b - i) % b));\n        ++count[i];\n      }\n      return cache[count] = ans;\n    };    \n    return count[0] + dp(0);\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">C++\/OPT<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua, 0 ms, 8.1 MB\nclass Solution {\npublic:\n  int maxHappyGroups(int b, vector<int>& groups) {\n    int ans = 0;\n    vector<int> count(b);\n    for (int g : groups) {\n      const int r = g % b;\n      if (r == 0) { ++ans; continue; }      \n      if (count[b - r]) {\n        --count[b - r];\n        ++ans;\n      } else {\n        ++count[r];\n      }\n    }    \n    map<vector<int>, int> cache;\n    function<int(int, int)> dp = [&](int r, int n) {\n      if (n == 0) return 0;\n      auto it = cache.find(count);\n      if (it != cache.end()) return it->second;\n      int ans = 0;\n      for (int i = 1; i < b; ++i) {\n        if (!count[i]) continue;\n        --count[i];\n        ans = max(ans, (r == 0) + dp((r + b - i) % b, n - 1));\n        ++count[i];\n      }\n      return cache[count] = ans;\n    };\n    const int n = accumulate(begin(count), end(count), 0);\n    return ans + (n ? dp(0, n) : 0);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There is a donuts shop that bakes donuts in batches of&nbsp;batchSize. They have a rule where they must serve&nbsp;all&nbsp;of the donuts of a batch before&#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":[122,33,18,217,121],"class_list":["post-8306","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-combination","tag-dfs","tag-dp","tag-hard","tag-permutation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8306","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=8306"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8306\/revisions"}],"predecessor-version":[{"id":8319,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8306\/revisions\/8319"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}