{"id":3119,"date":"2018-07-13T19:17:52","date_gmt":"2018-07-14T02:17:52","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3119"},"modified":"2018-07-13T19:18:01","modified_gmt":"2018-07-14T02:18:01","slug":"leetcode-698-partition-to-k-equal-sum-subsets","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-698-partition-to-k-equal-sum-subsets\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 698. Partition to K Equal Sum Subsets"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<div class=\"question-description__3U1T\">\n<div>\n<p>Given an array of integers\u00a0<code>nums<\/code>\u00a0and a positive integer\u00a0<code>k<\/code>, find whether it&#8217;s possible to divide this array into\u00a0<code>k<\/code>non-empty subsets whose sums are all equal.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b> nums = [4, 3, 2, 3, 5, 2, 1], k = 4\r\n<b>Output:<\/b> True\r\n<b>Explanation:<\/b> It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ul>\n<li><code>1 &lt;= k &lt;= len(nums) &lt;= 16<\/code>.<\/li>\n<li><code>0 &lt; nums[i] &lt; 10000<\/code>.<\/li>\n<\/ul>\n<h1><strong>Solution: Search<\/strong><\/h1>\n<p>Time complexity: O(n!)<\/p>\n<p>Space complexity: O(n)<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms (&lt;99.21%)\r\nclass Solution {\r\npublic:\r\n  bool canPartitionKSubsets(vector&lt;int&gt;&amp; nums, int k) {\r\n    const int sum = accumulate(nums.begin(), nums.end(), 0);\r\n    if (sum % k != 0) return false;    \r\n    sort(nums.rbegin(), nums.rend());    \r\n    return dfs(nums, sum \/ k, 0, k, 0);\r\n  }\r\nprivate:\r\n  bool dfs(const vector&lt;int&gt;&amp; nums, int target, int cur, int k, int used) {\r\n    if (k == 0) return used == (1 &lt;&lt; nums.size()) - 1;\r\n    for (int i = 0; i &lt; nums.size(); ++i) {\r\n      if (used &amp; (1 &lt;&lt; i)) continue;\r\n      int t = cur + nums[i];\r\n      if (t &gt; target) break; \/\/ Important\r\n      int new_used = used | (1 &lt;&lt; i);\r\n      if (t == target &amp;&amp; dfs(nums, target, 0, k - 1, new_used)) return true; \r\n      else if (dfs(nums, target, t, k, new_used)) return true;\r\n    }\r\n    return false;\r\n  }  \r\n};<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given an array of integers\u00a0nums\u00a0and a positive integer\u00a0k, find whether it&#8217;s possible to divide this array into\u00a0knon-empty subsets whose sums are all equal. Example&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[33,177,42,62],"class_list":["post-3119","post","type-post","status-publish","format-standard","hentry","category-searching","tag-dfs","tag-medium","tag-search","tag-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3119","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=3119"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3119\/revisions"}],"predecessor-version":[{"id":3121,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3119\/revisions\/3121"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}