{"id":1259,"date":"2017-12-15T23:33:12","date_gmt":"2017-12-16T07:33:12","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1259"},"modified":"2017-12-17T19:20:57","modified_gmt":"2017-12-18T03:20:57","slug":"leetcode-377-combination-sum-iv","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-377-combination-sum-iv\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 377. Combination Sum IV"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 377. Combination Sum IV - \u5237\u9898\u627e\u5de5\u4f5c EP135\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/niZlmOtG4jM?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><strong>Problem:<\/strong><\/p>\n<p>Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.<\/p>\n<p><b>Example:<\/b><\/p>\n<pre class=\"\">nums = [1, 2, 3]\r\ntarget = 4\r\n\r\nThe possible combination ways are:\r\n(1, 1, 1, 1)\r\n(1, 1, 2)\r\n(1, 2, 1)\r\n(1, 3)\r\n(2, 1, 1)\r\n(2, 2)\r\n(3, 1)\r\n\r\nNote that different sequences are counted as different combinations.\r\n\r\nTherefore the output is 7.<\/pre>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e9b\u6570\u548c\u4e00\u4e2a\u76ee\u6807\u503c\uff0c\u95ee\u4f7f\u7528\u8fd9\u4e9b\u6570(\u6bcf\u4e2a\u6570\u53ef\u4ee5\u88ab\u4f7f\u7528\u4efb\u610f\u6b21\u6570)\u52a0\u8d77\u6765\u7b49\u4e8e\u76ee\u6807\u503c\u7684\u7ec4\u5408\uff08\u5176\u5b9e\u662f\u4e0d\u91cd\u590d\u7684\u6392\u5217\uff09\u6709\u591a\u5c11\u79cd\u3002<\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p>DP<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1269\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/377-ep135.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/377-ep135.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/377-ep135-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/377-ep135-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>C++ \/ Recursion + Memorization<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Runtime: 0 ms\r\nclass Solution {\r\npublic:\r\n    int combinationSum4(vector&lt;int&gt;&amp; nums, int target) {\r\n        m_ = vector&lt;int&gt;(target + 1, -1);\r\n        m_[0] = 1;\r\n        return dp(nums, target);\r\n    }    \r\nprivate:\r\n    int dp(const vector&lt;int&gt;&amp; nums, int target) {\r\n        if (target &lt; 0) return 0;\r\n        if (m_[target] != -1) return m_[target];\r\n        int ans = 0;\r\n        for (const int num : nums)\r\n            ans += dp(nums, target - num);\r\n        return m_[target] = ans;\r\n    }\r\n    vector&lt;int&gt; m_;\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>C++ \/ DP<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Runtime: 3 ms\r\nclass Solution {\r\npublic:\r\n    int combinationSum4(vector&lt;int&gt;&amp; nums, int target) {\r\n        vector&lt;int&gt; dp(target + 1, 0); \/\/ dp[i] # of combinations sum up to i\r\n        dp[0] = 1;\r\n        for (int i = 1; i &lt;= target; ++i)\r\n            for (const int num : nums)\r\n                if (i - num &gt;= 0)\r\n                    dp[i] += dp[i - num];           \r\n        return dp[target];\r\n    }\r\n};<\/pre>\n<p><strong>Related Problems:<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-39-combination-sum\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 39. Combination Sum<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-40-combination-sum-ii\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 40. Combination Sum II<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-216-combination-sum-iii\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 216. Combination Sum III<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer&#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":[8,18],"class_list":["post-1259","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","category-medium","tag-counting","tag-dp","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1259","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=1259"}],"version-history":[{"count":7,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1259\/revisions"}],"predecessor-version":[{"id":1279,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1259\/revisions\/1279"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}