{"id":1910,"date":"2018-03-02T21:57:43","date_gmt":"2018-03-03T05:57:43","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1910"},"modified":"2018-03-02T22:24:36","modified_gmt":"2018-03-03T06:24:36","slug":"leetcode-518-coin-change-2","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-518-coin-change-2\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 518. Coin Change 2"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e9b\u786c\u5e01\u7684\u9762\u503c\uff0c\u95ee\u4f7f\u7528\u8fd9\u4e9b\u786c\u5e01\uff08\u65e0\u9650\u591a\u5757\uff09\u80fd\u591f\u7ec4\u6210amount\u7684\u65b9\u6cd5\u6709\u591a\u5c11\u79cd\u3002<\/p>\n<p>You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite number of each kind of coin.<\/p>\n<p><b>Note:<\/b>\u00a0You can assume that<\/p>\n<ul>\n<li>0 &lt;= amount &lt;= 5000<\/li>\n<li>1 &lt;= coin &lt;= 5000<\/li>\n<li>the number of coins is less than 500<\/li>\n<li>the answer is guaranteed to fit into signed 32-bit integer<\/li>\n<\/ul>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">Input: amount = 5, coins = [1, 2, 5]\r\nOutput: 4\r\nExplanation: there are four ways to make up the amount:\r\n5=5\r\n5=2+2+1\r\n5=2+1+1+1\r\n5=1+1+1+1+1\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"\">Input: amount = 3, coins = [2]\r\nOutput: 0\r\nExplanation: the amount of 3 cannot be made up just with coins of 2.\r\n<\/pre>\n<p><b>Example 3:<\/b><\/p>\n<pre class=\"\">Input: amount = 10, coins = [10] \r\nOutput: 1<\/pre>\n<p><strong>Idea: DP<\/strong><\/p>\n<p><strong>Transition 1:<\/strong><\/p>\n<p>Let us use dp[i][j] to denote the number of ways to sum up to amount j using first i kind of coins.<\/p>\n<p>dp[i][j] = dp[i &#8211; 1][j &#8211; coin] + dp[i &#8211; 1][j &#8211; 2* coin] + &#8230;<\/p>\n<p>Time complexity: O(n*amount^2) TLE<\/p>\n<p>Space complexity: O(n*amount) -&gt; O(amount)<\/p>\n<p><strong>Transition 2:<\/strong><\/p>\n<p>Let us use dp[i] to denote the number of ways to sum up to amount i.<\/p>\n<p>dp[i + coin] += dp[i]<\/p>\n<p>Time complexity: O(n*amount)<\/p>\n<p>Space complexity:\u00a0 O(amount)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  int change(int amount, vector&lt;int&gt;&amp; coins) {\r\n    vector&lt;int&gt; dp(amount + 1, 0);\r\n    dp[0] = 1;\r\n    for (const int coin : coins)\r\n      for (int i = 0; i &lt;= amount - coin; ++i)\r\n        dp[i + coin] += dp[i];      \r\n    return dp[amount];\r\n  }\r\n};<\/pre>\n<p>Java<\/p>\n<pre class=\"lang:java decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 7 ms\r\nclass Solution {\r\n  public int change(int amount, int[] coins) {\r\n    int[] dp = new int[amount + 1];\r\n    dp[0] = 1;\r\n    for (int coin : coins)\r\n      for (int i = 0; i &lt;= amount - coin; ++i)\r\n        dp[i + coin] += dp[i];\r\n    return dp[amount];\r\n  }\r\n}<\/pre>\n<p>Python<\/p>\n<pre class=\"lang:python decode:true \">\"\"\"\r\nAuthor: Huahua\r\nRunning time: 107 ms (beats 100%)\r\n\"\"\"\r\nclass Solution(object):\r\n  def change(self, amount, coins):\r\n    dp = [1] + [0] * (amount);    \r\n    for coin in coins:\r\n      for i in xrange(amount - coin + 1):\r\n        if dp[i]:\r\n          dp[i + coin] += dp[i]\r\n    return dp[amount]<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Related Problems:<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-322-coin-change\/\">\u82b1\u82b1\u9171 LeetCode 322. Coin Change<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e9b\u786c\u5e01\u7684\u9762\u503c\uff0c\u95ee\u4f7f\u7528\u8fd9\u4e9b\u786c\u5e01\uff08\u65e0\u9650\u591a\u5757\uff09\u80fd\u591f\u7ec4\u6210amount\u7684\u65b9\u6cd5\u6709\u591a\u5c11\u79cd\u3002 You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make&#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":[8,18,195],"class_list":["post-1910","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-counting","tag-dp","tag-knapsack","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1910","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=1910"}],"version-history":[{"count":8,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1910\/revisions"}],"predecessor-version":[{"id":1913,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1910\/revisions\/1913"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}