{"id":1381,"date":"2017-12-31T14:41:36","date_gmt":"2017-12-31T22:41:36","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1381"},"modified":"2018-01-01T13:43:52","modified_gmt":"2018-01-01T21:43:52","slug":"leetcode-416-partition-equal-subset-sum","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-416-partition-equal-subset-sum\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 416. Partition Equal Subset Sum"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 416. Partition Equal Subset Sum - \u5237\u9898\u627e\u5de5\u4f5c EP145\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/r6I-ikllNDM?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<p>\u9898\u76ee\u5927\u610f\uff1a<\/p>\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6b63\u6574\u6570\u7684\u6570\u7ec4\uff0c\u95ee\u4f60\u80fd\u5426\u628a\u5143\u7d20\u5212\u5206\u6210\u5143\u7d20\u548c\u76f8\u7b49\u7684\u4e24\u7ec4\u3002<\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>Given a\u00a0<b>non-empty<\/b>\u00a0array containing\u00a0<b>only positive integers<\/b>, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.<\/p>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>Each of the array element will not exceed 100.<\/li>\n<li>The array size will not exceed 200.<\/li>\n<\/ol>\n<p><b>Example 1:<\/b><\/p>\n<pre>Input: [1, 5, 11, 5]\r\n\r\nOutput: true\r\n\r\nExplanation: The array can be partitioned as [1, 5, 5] and [11].\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"\">Input: [1, 2, 3, 5]\r\n\r\nOutput: false\r\n\r\nExplanation: The array cannot be partitioned into equal sum subsets.<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p>DP \u52a8\u6001\u89c4\u5212<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>Time complexity: O(n*sum)<\/p>\n<p>Space complexity: O(sum)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 16 ms\r\nclass Solution {\r\npublic:\r\n    bool canPartition(vector&lt;int&gt;&amp; nums) {\r\n        const int sum = std::accumulate(nums.begin(), nums.end(), 0);\r\n        if (sum % 2 != 0) return false;\r\n        vector&lt;int&gt; dp(sum + 1, 0);\r\n        dp[0] = 1;\r\n        for (const int num : nums) {\r\n            for (int i = sum; i &gt;= 0; --i)\r\n                if (dp[i]) dp[i + num] = 1;\r\n            if (dp[sum \/ 2]) return true;\r\n        }\r\n        return false;\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a \u7ed9\u4f60\u4e00\u4e2a\u6b63\u6574\u6570\u7684\u6570\u7ec4\uff0c\u95ee\u4f60\u80fd\u5426\u628a\u5143\u7d20\u5212\u5206\u6210\u5143\u7d20\u548c\u76f8\u7b49\u7684\u4e24\u7ec4\u3002 Problem: Given a\u00a0non-empty\u00a0array containing\u00a0only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in&#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,164],"tags":[18,195],"class_list":["post-1381","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","category-medium","tag-dp","tag-knapsack","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1381","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=1381"}],"version-history":[{"count":8,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1381\/revisions"}],"predecessor-version":[{"id":1418,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1381\/revisions\/1418"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}