{"id":3364,"date":"2018-07-28T23:06:45","date_gmt":"2018-07-29T06:06:45","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3364"},"modified":"2018-07-29T09:17:26","modified_gmt":"2018-07-29T16:17:26","slug":"leetcode-879-profitable-schemes","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-879-profitable-schemes\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 879. Profitable Schemes"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 879. Profitable Schemes - \u5237\u9898\u627e\u5de5\u4f5c EP212\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/MjOIR61txFc?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><\/p>\n<h1><strong>Problem<\/strong><\/h1>\n<p>There are G people in a gang, and a list of various crimes they could commit.<\/p>\n<p>The\u00a0<code>i<\/code>-th crime generates a\u00a0<code>profit[i]<\/code>\u00a0and requires\u00a0<code>group[i]<\/code>\u00a0gang members to participate.<\/p>\n<p>If a gang member participates in one crime, that member can&#8217;t participate in another crime.<\/p>\n<p>Let&#8217;s call a\u00a0<em>profitable\u00a0scheme<\/em>\u00a0any subset of these crimes that generates at least\u00a0<code>P<\/code>\u00a0profit, and the total number of gang members participating in that subset of crimes is at most G.<\/p>\n<p>How many schemes can be chosen?\u00a0 Since the answer may be very\u00a0large,\u00a0<strong>return it modulo<\/strong>\u00a0<code>10^9 + 7<\/code>.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>G = <span id=\"example-input-1-1\">5<\/span>, P = <span id=\"example-input-1-2\">3<\/span>, group = <span id=\"example-input-1-3\">[2,2]<\/span>, profit = <span id=\"example-input-1-4\">[2,3]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">2<\/span>\r\n<strong>Explanation: <\/strong>\r\nTo make a profit of at least 3, the gang could either commit crimes 0 and 1, or just crime 1.\r\nIn total, there are 2 schemes.\r\n<\/pre>\n<div>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false \"><strong>Input: <\/strong>G = <span id=\"example-input-2-1\">10<\/span>, P = <span id=\"example-input-2-2\">5<\/span>, group = <span id=\"example-input-2-3\">[2,3,5]<\/span>, profit = <span id=\"example-input-2-4\">[6,7,8]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">7<\/span>\r\n<strong>Explanation: <\/strong>\r\nTo make a profit of at least 5, the gang could commit any crimes, as long as they commit one.\r\nThere are 7 possible schemes: (0), (1), (2), (0,1), (0,2), (1,2), and (0,1,2).\r\n<\/pre>\n<\/div>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>1 &lt;= G &lt;= 100<\/code><\/li>\n<li><code>0 &lt;= P &lt;= 100<\/code><\/li>\n<li><code>1 &lt;= group[i] &lt;= 100<\/code><\/li>\n<li><code>0 &lt;= profit[i] &lt;= 100<\/code><\/li>\n<li><code>1 &lt;= group.length = profit.length &lt;= 100<\/code><\/li>\n<\/ol>\n<h1><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3372\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/07\/879-ep212.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/07\/879-ep212.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/07\/879-ep212-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/07\/879-ep212-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/h1>\n<h1><strong>Solution: DP<\/strong><\/h1>\n<p>Time complexity: O(KPG)<\/p>\n<p>Space complexity: O(KPG)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 64 ms\r\nclass Solution {\r\npublic:\r\n  int profitableSchemes(int G, int P, vector&lt;int&gt;&amp; group, vector&lt;int&gt;&amp; profit) {    \r\n    const int kMod = 1000000007;\r\n    const int K = group.size();\r\n    \/\/ dp[k][i][j]:= # of schemes of making profit i with j people by commiting first k crimes.\r\n    vector&lt;vector&lt;vector&lt;int&gt;&gt;&gt; dp(K + 1, vector&lt;vector&lt;int&gt;&gt;(P + 1, vector&lt;int&gt;(G + 1, 0)));\r\n    dp[0][0][0] = 1;\r\n    \r\n    for (int k = 1; k &lt;= K; ++k) {      \r\n      const int p = profit[k - 1];\r\n      const int g = group[k - 1];\r\n      for (int i = 0; i &lt;= P; ++i)    \r\n        for (int j = 0; j &lt;= G; ++j)\r\n          dp[k][i][j] = (dp[k - 1][i][j] + (j &lt; g ? 0 : dp[k - 1][max(0, i - p)][j - g])) % kMod;\r\n    }\r\n    return accumulate(begin(dp[K][P]), end(dp[K][P]), 0LL) % kMod;\r\n  }\r\n};<\/pre>\n<p>Space complexity: O(PG)<\/p>\n<p>v1: Dimension reduction by copying.<\/p>\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 24 ms\r\nclass Solution {\r\npublic:\r\n  int profitableSchemes(int G, int P, vector&lt;int&gt;&amp; group, vector&lt;int&gt;&amp; profit) {    \r\n    const int kMod = 1000000007;\r\n    const int K = group.size();\r\n    \/\/ dp[i][j]:= # of schemes of making profit i with j people.\r\n    vector&lt;vector&lt;int&gt;&gt; dp(P + 1, vector&lt;int&gt;(G + 1, 0));\r\n    dp[0][0] = 1;\r\n    \r\n    for (int k = 1; k &lt;= K; ++k) {\r\n      auto tmp = dp;\r\n      const int p = profit[k - 1];\r\n      const int g = group[k - 1];\r\n      for (int i = 0; i &lt;= P; ++i)    \r\n        for (int j = 0; j &lt;= G; ++j)\r\n          tmp[i][j] = (tmp[i][j] + (j &lt; g ? 0 : dp[max(0, i - p)][j - g])) % kMod;\r\n      dp.swap(tmp);\r\n    }\r\n    return accumulate(begin(dp[P]), end(dp[P]), 0LL) % kMod;\r\n  }\r\n};<\/pre>\n<p>v2: Dimension reduction by using rolling array.<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 16 ms\r\nclass Solution {\r\npublic:\r\n  int profitableSchemes(int G, int P, vector&lt;int&gt;&amp; group, vector&lt;int&gt;&amp; profit) {\r\n    const int kMod = 1000000007;\r\n    \/\/ dp[i][j]:= # of schemes of making profit i with j people.\r\n    vector&lt;vector&lt;int&gt;&gt; dp(P + 1, vector&lt;int&gt;(G + 1, 0));\r\n    dp[0][0] = 1;\r\n    const int K = group.size();\r\n    for (int k = 0; k &lt; K; ++k) {\r\n      const int p = profit[k];\r\n      const int g = group[k];\r\n      for (int i = P; i &gt;= 0; --i) {\r\n        const int ip = min(P, i + p);\r\n        for (int j = G - g; j &gt;= 0; --j)\r\n          dp[ip][j + g] = (dp[ip][j + g] + dp[i][j]) % kMod;\r\n      }\r\n    }\r\n    return accumulate(begin(dp[P]), end(dp[P]), 0LL) % kMod;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem There are G people in a gang, and a list of various crimes they could commit. The\u00a0i-th crime generates a\u00a0profit[i]\u00a0and requires\u00a0group[i]\u00a0gang members to participate.&#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":[337,18,217],"class_list":["post-3364","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-0-1-knapsack","tag-dp","tag-hard","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3364","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=3364"}],"version-history":[{"count":8,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3364\/revisions"}],"predecessor-version":[{"id":3373,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3364\/revisions\/3373"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}