{"id":174,"date":"2020-04-26T06:11:07","date_gmt":"2020-04-26T06:11:07","guid":{"rendered":"http:\/\/feellikelearning.com\/?p=174"},"modified":"2020-12-20T20:12:30","modified_gmt":"2020-12-20T20:12:30","slug":"leetcode-322-coin-change-solution","status":"publish","type":"post","link":"https:\/\/feellikelearning.com\/index.php\/2020\/04\/26\/leetcode-322-coin-change-solution\/","title":{"rendered":"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 322. Coin Change \u89e3\u6cd5"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p>\u9898\u76ee\u5728<a href=\"https:\/\/leetcode.com\/problems\/coin-change\/\">\u8fd9\u91cc<\/a>\u3002<\/p>\n\n\n\n<p>\u81ea\u5df1\u60f3\u7684\u7b54\u6848\uff0cbottom up dp\u4e0d\u592a\u597d\u60f3\uff0c\u4ecetop down + cache\u5f00\u59cb\uff0c\u5199\u4e86\u51e0\u6b21\uff0c\u6bcf\u6b21\u7b80\u5316\u4e00\u70b9\uff0c\u6700\u7ec8\u5199\u6210\u8fd9\u6837\u901a\u8fc7OJ\u3002<\/p>\n\n\n\n<p>Python \u7248\u672c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution(object):\n    def coinChange(self, coins, amount):\n        \"\"\"\n        :type coins: List&#91;int]\n        :type amount: int\n        :rtype: int\n        \"\"\"\n        cache = {0:0}\n        self.dfs(coins, amount, cache)\n        return cache&#91;amount]\n        \n    def dfs(self, coins, amount, cache):\n        if amount in cache:\n            return cache&#91;amount]\n        if amount == 0:\n            return 0\n        if amount &lt; 0:\n            return -1\n        ans = float('inf')\n        for coin in coins:\n            rest_cnt = self.dfs(coins, amount - coin, cache)\n            if rest_cnt != -1:\n                ans = min(ans, rest_cnt + 1)\n        if ans == float('inf'):\n            cache&#91;amount] = -1\n            return -1\n        \n        cache&#91;amount] = ans\n        return ans <\/code><\/pre>\n\n\n\n<p>\u4e3a\u4e86\u66f4\u597d\u7406\u89e3\u7b97\u6cd5\uff0c\u52a0\u4e86\u4e9blog\uff0c\u8bb0\u5f55dfs\u88abcall\u4e86\u51e0\u6b21\u548ccache hit\uff0c\u548c\u6bcf\u4e2acache key\u88ab\u5199\u5165\u6b21\u6570\uff08\u4f7f\u7528cache\u548c\u4e0d\u7528cache\u7684\u60c5\u51b5\u3002<\/p>\n\n\n\n<p>\u7528coins = [1, 2,  5]\u548camount = 11\u4e3a\u4f8b\u5b50<\/p>\n\n\n\n<p>\u7528\u4e86cache<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dfs call: 34\ncache hit: 15\nkey write times\n1: 1\n2: 1\n3: 1\n4: 1\n5: 1\n6: 1\n7: 1\n8: 1\n9: 1\n10: 1\n11: 1<\/code><\/pre>\n\n\n\n<p>\u4e0d\u7528cache<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dfs call: 928\ncache hit: 0\nkey write times\n1: 128\n2: 75\n3: 44\n4: 26\n5: 15\n6: 9\n7: 5\n8: 3\n9: 2\n10: 1\n11: 1<\/code><\/pre>\n\n\n\n<p>\u8fd0\u884c\u5b8c\u540ecache\u7684\u5185\u5bb9\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0: 0\n1: 1\n2: 1\n3: 2\n4: 2\n5: 1\n6: 2\n7: 2\n8: 3\n9: 3\n10: 2\n11: 3<\/code><\/pre>\n\n\n\n<p>\u6b63\u662f\u4ece0\u523011\u6bcf\u4e2aamount\u7684\u6700\u5c11\u786c\u5e01\u6570\u3002\u4ece\u6b64\u51fa\u53d1\uff0c\u60f3\u5230bottom up dp\u6b63\u662f\u9700\u8981\u6784\u5efa\u5982\u4e0acache\u7684\u5185\u5bb9\u3002dp[0] = 0\uff0camount\u662f0\uff0c\u5f53\u7136\u4e00\u4e2a\u786c\u5e01\u4e0d\u7528\u3002dp[amount]\u5c31\u770bamount &#8211; coin\u503c\u662f\u4e0d\u662f\u6b63\uff0c\u800c\u4e14\u4e4b\u524d\u662f\u5426\u6709\u975e-1\u7684\u503c\u3002\u5f53\u7b97\u5230amount\u7684\u65f6\u5019\uff0c\u524d\u9762dp[0]\u5230d][amount-coin]\u90fd\u7b97\u8fc7\u4e86\u3002\u5bf9\u6240\u6709\u7684coin\u503c\u90fd\u7b97\u4e00\u904d\uff0c\u628a\u6240\u6709dp[amount &#8211; coin]\u7684\u975e-1\u6700\u5c0f\u503c\u627e\u51fa\u6765\uff0c\u52a01\uff0c\u5c31\u662fdp[amount]\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(amount * len(coins))\u3002\u7a7a\u95f4\u4e3a O(amount)\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution(object):\n    def coinChange(self, coins, amount):\n        \"\"\"\n        :type coins: List&#91;int]\n        :type amount: int\n        :rtype: int\n        \"\"\"\n        dp = &#91;0] * (amount + 1)\n        for i in range(1, amount + 1):\n            rest_min_cnt = -1\n            for coin in coins:\n                rest = i - coin\n                if rest &gt;= 0 and dp&#91;rest] != -1:\n                    if rest_min_cnt == -1:\n                        rest_min_cnt = dp&#91;rest]\n                    else:\n                        rest_min_cnt = min(rest_min_cnt, dp&#91;rest])\n            dp&#91;i] = rest_min_cnt + 1 if rest_min_cnt != -1 else -1\n            \n        \n        return dp&#91;amount]                                    <\/code><\/pre>\n\n\n\n<p>\u8fc7\u4e86\u591a\u65e5\u4ee5\u540e\u81ea\u5df1\u91cd\u65b0\u60f3\u7684Python\u5199\u6cd5, \u770b\u770b\u662f\u5426\u5df2\u7ecf\u5185\u5316\u5e76\u638c\u63e1. \u5bf9\u6bd4\u4e0a\u9762, \u505a\u4e86\u4e00\u4e9b\u6539\u8fdb, dp list init\u6210\u5168\u90e8-1, \u4ee3\u8868\u4e00\u5f00\u59cb\u90fd\u4e0d\u53ef\u80fd. <code>dp[0]<\/code>\u8bbe\u62100, \u4ee3\u8868\u4e00\u4e2acoin\u90fd\u4e0d\u53d6. \u65b0\u89e3\u6cd5\u7701\u4e86<code>rest_min_cnt<\/code>\u4e00\u4e2a\u53d8\u91cf. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution(object):\n    def coinChange(self, coins, amount):\n        \"\"\"\n        :type coins: List&#91;int]\n        :type amount: int\n        :rtype: int\n        \"\"\"\n        dp = &#91;-1] * (amount + 1)\n        dp&#91;0] = 0\n        for i in range(1, amount + 1):\n            for j in coins:\n                k = i - j\n                if k &gt;= 0 and dp&#91;k] != -1:\n                    if dp&#91;i] == -1:\n                        dp&#91;i] = dp&#91;k] + 1\n                    else:\n                        dp&#91;i] = min(dp&#91;i], dp&#91;k] + 1)\n        return dp&#91;amount]\n        <\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1758\" height=\"410\" src=\"http:\/\/feellikelearning.com\/wp-content\/uploads\/2020\/12\/image-25.png\" alt=\"\" class=\"wp-image-1062\"\/><\/figure>\n\n\n\n<p>Java\u7248\u672c\u7684bottom up dp\u89e3\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\n    public int coinChange(int&#91;] coins, int amount) {\n        int&#91;] dp = new int&#91;amount + 1];\n        for (int i = 1; i &lt;= amount; i++) {\n            int restMinCoins = -1;\n            for (int coin : coins) {\n                int remainder = i - coin;\n                if (remainder &gt;= 0 &amp;&amp; dp&#91;remainder] != -1) {\n                    if (restMinCoins == -1) {\n                        restMinCoins = dp&#91;remainder];\n                    } else {\n                        restMinCoins = Math.min(restMinCoins, dp&#91;remainder]);\n                    }\n                }\n            }\n            if (restMinCoins == -1) {\n                dp&#91;i] = -1;\n            } else {\n                dp&#91;i] = restMinCoins + 1;\n            }\n        }\n        return dp&#91;amount];\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5728\u8fd9\u91cc\u3002 \u81ea\u5df1\u60f3\u7684\u7b54\u6848\uff0cbottom up dp\u4e0d\u592a\u597d\u60f3\uff0c\u4ecetop down + cache\u5f00\u59cb\uff0c\u5199\u4e86\u51e0\u6b21\uff0c\u6bcf\u6b21\u7b80\u5316\u4e00\u70b9\uff0c\u6700\u7ec8\u5199\u6210\u8fd9\u6837\u901a\u8fc7OJ\u3002 Python \u7248\u672c\u3002 \u4e3a\u4e86\u66f4\u597d\u7406\u89e3\u7b97\u6cd5\uff0c\u52a0\u4e86\u4e9blog\uff0c\u8bb0\u5f55dfs\u88abcall\u4e86\u51e0\u6b21\u548ccache hit\uff0c\u548c\u6bcf\u4e2acache key\u88ab\u5199\u5165\u6b21\u6570\uff08\u4f7f\u7528cache\u548c\u4e0d\u7528cache\u7684\u60c5\u51b5\u3002 \u7528coins = [1, 2, 5]\u548camount = 11\u4e3a\u4f8b\u5b50 \u7528\u4e86cache \u4e0d\u7528cache \u8fd0\u884c\u5b8c\u540ecache\u7684\u5185\u5bb9\u5982\u4e0b \u6b63\u662f\u4ece0\u523011\u6bcf\u4e2aamount\u7684\u6700\u5c11\u786c\u5e01\u6570\u3002\u4ece\u6b64\u51fa\u53d1\uff0c\u60f3\u5230bottom up dp\u6b63\u662f\u9700\u8981\u6784\u5efa\u5982\u4e0acache\u7684\u5185\u5bb9\u3002dp[0] = 0\uff0camount\u662f0\uff0c\u5f53\u7136\u4e00\u4e2a\u786c\u5e01\u4e0d\u7528\u3002dp[amount]\u5c31\u770bamount &#8211; coin\u503c\u662f\u4e0d\u662f\u6b63\uff0c\u800c\u4e14\u4e4b\u524d\u662f\u5426\u6709\u975e-1\u7684\u503c\u3002\u5f53\u7b97\u5230amount\u7684\u65f6\u5019\uff0c\u524d\u9762dp[0]\u5230d][amount-coin]\u90fd\u7b97\u8fc7\u4e86\u3002\u5bf9\u6240\u6709\u7684coin\u503c\u90fd\u7b97\u4e00\u904d\uff0c\u628a\u6240\u6709dp[amount &#8211; coin]\u7684\u975e-1\u6700\u5c0f\u503c\u627e\u51fa\u6765\uff0c\u52a01\uff0c\u5c31\u662fdp[amount]\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(amount * len(coins))\u3002\u7a7a\u95f4\u4e3a O(amount)\u3002 \u8fc7\u4e86\u591a\u65e5\u4ee5\u540e\u81ea\u5df1\u91cd\u65b0\u60f3\u7684Python\u5199\u6cd5, \u770b\u770b\u662f\u5426\u5df2\u7ecf\u5185\u5316\u5e76\u638c\u63e1. \u5bf9\u6bd4\u4e0a\u9762, \u505a\u4e86\u4e00\u4e9b\u6539\u8fdb, dp list&#8230;<br \/><a class=\"read-more-button\" href=\"https:\/\/feellikelearning.com\/index.php\/2020\/04\/26\/leetcode-322-coin-change-solution\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":1065,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[19,16,15,17],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.10 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 322. Coin Change \u89e3\u6cd5 | Feel Like Learning<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/feellikelearning.com\/index.php\/2020\/04\/26\/leetcode-322-coin-change-solution\/?variant=zh-hant\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 322. Coin Change \u89e3\u6cd5 | Feel Like Learning\" \/>\n<meta property=\"og:description\" content=\"\u9898\u76ee\u5728\u8fd9\u91cc\u3002 \u81ea\u5df1\u60f3\u7684\u7b54\u6848\uff0cbottom up dp\u4e0d\u592a\u597d\u60f3\uff0c\u4ecetop down + cache\u5f00\u59cb\uff0c\u5199\u4e86\u51e0\u6b21\uff0c\u6bcf\u6b21\u7b80\u5316\u4e00\u70b9\uff0c\u6700\u7ec8\u5199\u6210\u8fd9\u6837\u901a\u8fc7OJ\u3002 Python \u7248\u672c\u3002 \u4e3a\u4e86\u66f4\u597d\u7406\u89e3\u7b97\u6cd5\uff0c\u52a0\u4e86\u4e9blog\uff0c\u8bb0\u5f55dfs\u88abcall\u4e86\u51e0\u6b21\u548ccache hit\uff0c\u548c\u6bcf\u4e2acache key\u88ab\u5199\u5165\u6b21\u6570\uff08\u4f7f\u7528cache\u548c\u4e0d\u7528cache\u7684\u60c5\u51b5\u3002 \u7528coins = [1, 2, 5]\u548camount = 11\u4e3a\u4f8b\u5b50 \u7528\u4e86cache \u4e0d\u7528cache \u8fd0\u884c\u5b8c\u540ecache\u7684\u5185\u5bb9\u5982\u4e0b \u6b63\u662f\u4ece0\u523011\u6bcf\u4e2aamount\u7684\u6700\u5c11\u786c\u5e01\u6570\u3002\u4ece\u6b64\u51fa\u53d1\uff0c\u60f3\u5230bottom up dp\u6b63\u662f\u9700\u8981\u6784\u5efa\u5982\u4e0acache\u7684\u5185\u5bb9\u3002dp[0] = 0\uff0camount\u662f0\uff0c\u5f53\u7136\u4e00\u4e2a\u786c\u5e01\u4e0d\u7528\u3002dp[amount]\u5c31\u770bamount &#8211; coin\u503c\u662f\u4e0d\u662f\u6b63\uff0c\u800c\u4e14\u4e4b\u524d\u662f\u5426\u6709\u975e-1\u7684\u503c\u3002\u5f53\u7b97\u5230amount\u7684\u65f6\u5019\uff0c\u524d\u9762dp[0]\u5230d][amount-coin]\u90fd\u7b97\u8fc7\u4e86\u3002\u5bf9\u6240\u6709\u7684coin\u503c\u90fd\u7b97\u4e00\u904d\uff0c\u628a\u6240\u6709dp[amount &#8211; coin]\u7684\u975e-1\u6700\u5c0f\u503c\u627e\u51fa\u6765\uff0c\u52a01\uff0c\u5c31\u662fdp[amount]\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(amount * len(coins))\u3002\u7a7a\u95f4\u4e3a O(amount)\u3002 \u8fc7\u4e86\u591a\u65e5\u4ee5\u540e\u81ea\u5df1\u91cd\u65b0\u60f3\u7684Python\u5199\u6cd5, \u770b\u770b\u662f\u5426\u5df2\u7ecf\u5185\u5316\u5e76\u638c\u63e1. \u5bf9\u6bd4\u4e0a\u9762, \u505a\u4e86\u4e00\u4e9b\u6539\u8fdb, dp list...Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/feellikelearning.com\/index.php\/2020\/04\/26\/leetcode-322-coin-change-solution\/?variant=zh-hant\" \/>\n<meta property=\"og:site_name\" content=\"Feel Like Learning\" \/>\n<meta property=\"article:published_time\" content=\"2020-04-26T06:11:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-20T20:12:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/feellikelearning.com\/wp-content\/uploads\/2020\/04\/blog-covers.020.jpeg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"feellikelearning\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"feellikelearning\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/04\/26\/leetcode-322-coin-change-solution\/?variant=zh-hant#article\",\"isPartOf\":{\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/04\/26\/leetcode-322-coin-change-solution\/?variant=zh-hant\"},\"author\":{\"name\":\"feellikelearning\",\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\"},\"headline\":\"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 322. Coin Change \u89e3\u6cd5\",\"datePublished\":\"2020-04-26T06:11:07+00:00\",\"dateModified\":\"2020-12-20T20:12:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/04\/26\/leetcode-322-coin-change-solution\/?variant=zh-hant\"},\"wordCount\":66,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\"},\"articleSection\":[\"leetcode\",\"\u52a8\u6001\u89c4\u5212\",\"\u7b97\u6cd5\",\"\u80cc\u5305\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/feellikelearning.com\/index.php\/2020\/04\/26\/leetcode-322-coin-change-solution\/?variant=zh-hant#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/04\/26\/leetcode-322-coin-change-solution\/?variant=zh-hant\",\"url\":\"https:\/\/feellikelearning.com\/index.php\/2020\/04\/26\/leetcode-322-coin-change-solution\/?variant=zh-hant\",\"name\":\"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 322. Coin Change \u89e3\u6cd5 | Feel Like Learning\",\"isPartOf\":{\"@id\":\"https:\/\/feellikelearning.com\/#website\"},\"datePublished\":\"2020-04-26T06:11:07+00:00\",\"dateModified\":\"2020-12-20T20:12:30+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/04\/26\/leetcode-322-coin-change-solution\/?variant=zh-hant#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/feellikelearning.com\/index.php\/2020\/04\/26\/leetcode-322-coin-change-solution\/?variant=zh-hant\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/04\/26\/leetcode-322-coin-change-solution\/?variant=zh-hant#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/feellikelearning.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 322. Coin Change \u89e3\u6cd5\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/feellikelearning.com\/#website\",\"url\":\"https:\/\/feellikelearning.com\/\",\"name\":\"Feel Like Learning\",\"description\":\"\u7a0b\u5e8f\uff5c\u751f\u6d3b\uff5c\u5b66\u5230\u5c31\u662f\u8d5a\u5230\",\"publisher\":{\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/feellikelearning.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\",\"name\":\"feellikelearning\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g\",\"caption\":\"feellikelearning\"},\"logo\":{\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/\"},\"url\":\"https:\/\/feellikelearning.com\/index.php\/author\/feellikelearning\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 322. Coin Change \u89e3\u6cd5 | Feel Like Learning","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/feellikelearning.com\/index.php\/2020\/04\/26\/leetcode-322-coin-change-solution\/?variant=zh-hant","og_locale":"en_US","og_type":"article","og_title":"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 322. Coin Change \u89e3\u6cd5 | Feel Like Learning","og_description":"\u9898\u76ee\u5728\u8fd9\u91cc\u3002 \u81ea\u5df1\u60f3\u7684\u7b54\u6848\uff0cbottom up dp\u4e0d\u592a\u597d\u60f3\uff0c\u4ecetop down + cache\u5f00\u59cb\uff0c\u5199\u4e86\u51e0\u6b21\uff0c\u6bcf\u6b21\u7b80\u5316\u4e00\u70b9\uff0c\u6700\u7ec8\u5199\u6210\u8fd9\u6837\u901a\u8fc7OJ\u3002 Python \u7248\u672c\u3002 \u4e3a\u4e86\u66f4\u597d\u7406\u89e3\u7b97\u6cd5\uff0c\u52a0\u4e86\u4e9blog\uff0c\u8bb0\u5f55dfs\u88abcall\u4e86\u51e0\u6b21\u548ccache hit\uff0c\u548c\u6bcf\u4e2acache key\u88ab\u5199\u5165\u6b21\u6570\uff08\u4f7f\u7528cache\u548c\u4e0d\u7528cache\u7684\u60c5\u51b5\u3002 \u7528coins = [1, 2, 5]\u548camount = 11\u4e3a\u4f8b\u5b50 \u7528\u4e86cache \u4e0d\u7528cache \u8fd0\u884c\u5b8c\u540ecache\u7684\u5185\u5bb9\u5982\u4e0b \u6b63\u662f\u4ece0\u523011\u6bcf\u4e2aamount\u7684\u6700\u5c11\u786c\u5e01\u6570\u3002\u4ece\u6b64\u51fa\u53d1\uff0c\u60f3\u5230bottom up dp\u6b63\u662f\u9700\u8981\u6784\u5efa\u5982\u4e0acache\u7684\u5185\u5bb9\u3002dp[0] = 0\uff0camount\u662f0\uff0c\u5f53\u7136\u4e00\u4e2a\u786c\u5e01\u4e0d\u7528\u3002dp[amount]\u5c31\u770bamount &#8211; coin\u503c\u662f\u4e0d\u662f\u6b63\uff0c\u800c\u4e14\u4e4b\u524d\u662f\u5426\u6709\u975e-1\u7684\u503c\u3002\u5f53\u7b97\u5230amount\u7684\u65f6\u5019\uff0c\u524d\u9762dp[0]\u5230d][amount-coin]\u90fd\u7b97\u8fc7\u4e86\u3002\u5bf9\u6240\u6709\u7684coin\u503c\u90fd\u7b97\u4e00\u904d\uff0c\u628a\u6240\u6709dp[amount &#8211; coin]\u7684\u975e-1\u6700\u5c0f\u503c\u627e\u51fa\u6765\uff0c\u52a01\uff0c\u5c31\u662fdp[amount]\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(amount * len(coins))\u3002\u7a7a\u95f4\u4e3a O(amount)\u3002 \u8fc7\u4e86\u591a\u65e5\u4ee5\u540e\u81ea\u5df1\u91cd\u65b0\u60f3\u7684Python\u5199\u6cd5, \u770b\u770b\u662f\u5426\u5df2\u7ecf\u5185\u5316\u5e76\u638c\u63e1. \u5bf9\u6bd4\u4e0a\u9762, \u505a\u4e86\u4e00\u4e9b\u6539\u8fdb, dp list...Read more","og_url":"https:\/\/feellikelearning.com\/index.php\/2020\/04\/26\/leetcode-322-coin-change-solution\/?variant=zh-hant","og_site_name":"Feel Like Learning","article_published_time":"2020-04-26T06:11:07+00:00","article_modified_time":"2020-12-20T20:12:30+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/feellikelearning.com\/wp-content\/uploads\/2020\/04\/blog-covers.020.jpeg","type":"image\/jpeg"}],"author":"feellikelearning","twitter_card":"summary_large_image","twitter_misc":{"Written by":"feellikelearning","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/feellikelearning.com\/index.php\/2020\/04\/26\/leetcode-322-coin-change-solution\/?variant=zh-hant#article","isPartOf":{"@id":"https:\/\/feellikelearning.com\/index.php\/2020\/04\/26\/leetcode-322-coin-change-solution\/?variant=zh-hant"},"author":{"name":"feellikelearning","@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a"},"headline":"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 322. Coin Change \u89e3\u6cd5","datePublished":"2020-04-26T06:11:07+00:00","dateModified":"2020-12-20T20:12:30+00:00","mainEntityOfPage":{"@id":"https:\/\/feellikelearning.com\/index.php\/2020\/04\/26\/leetcode-322-coin-change-solution\/?variant=zh-hant"},"wordCount":66,"commentCount":0,"publisher":{"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a"},"articleSection":["leetcode","\u52a8\u6001\u89c4\u5212","\u7b97\u6cd5","\u80cc\u5305"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/feellikelearning.com\/index.php\/2020\/04\/26\/leetcode-322-coin-change-solution\/?variant=zh-hant#respond"]}]},{"@type":"WebPage","@id":"https:\/\/feellikelearning.com\/index.php\/2020\/04\/26\/leetcode-322-coin-change-solution\/?variant=zh-hant","url":"https:\/\/feellikelearning.com\/index.php\/2020\/04\/26\/leetcode-322-coin-change-solution\/?variant=zh-hant","name":"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 322. Coin Change \u89e3\u6cd5 | Feel Like Learning","isPartOf":{"@id":"https:\/\/feellikelearning.com\/#website"},"datePublished":"2020-04-26T06:11:07+00:00","dateModified":"2020-12-20T20:12:30+00:00","breadcrumb":{"@id":"https:\/\/feellikelearning.com\/index.php\/2020\/04\/26\/leetcode-322-coin-change-solution\/?variant=zh-hant#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/feellikelearning.com\/index.php\/2020\/04\/26\/leetcode-322-coin-change-solution\/?variant=zh-hant"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/feellikelearning.com\/index.php\/2020\/04\/26\/leetcode-322-coin-change-solution\/?variant=zh-hant#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/feellikelearning.com\/"},{"@type":"ListItem","position":2,"name":"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 322. Coin Change \u89e3\u6cd5"}]},{"@type":"WebSite","@id":"https:\/\/feellikelearning.com\/#website","url":"https:\/\/feellikelearning.com\/","name":"Feel Like Learning","description":"\u7a0b\u5e8f\uff5c\u751f\u6d3b\uff5c\u5b66\u5230\u5c31\u662f\u8d5a\u5230","publisher":{"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/feellikelearning.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a","name":"feellikelearning","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g","caption":"feellikelearning"},"logo":{"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/"},"url":"https:\/\/feellikelearning.com\/index.php\/author\/feellikelearning\/"}]}},"_links":{"self":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts\/174"}],"collection":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/comments?post=174"}],"version-history":[{"count":12,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts\/174\/revisions"}],"predecessor-version":[{"id":1066,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts\/174\/revisions\/1066"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/media\/1065"}],"wp:attachment":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/media?parent=174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/categories?post=174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/tags?post=174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}