{"id":192,"date":"2020-04-27T00:47:22","date_gmt":"2020-04-27T00:47:22","guid":{"rendered":"http:\/\/feellikelearning.com\/?p=192"},"modified":"2020-04-29T08:12:58","modified_gmt":"2020-04-29T08:12:58","slug":"leetcode-518-coin-change-2-solution","status":"publish","type":"post","link":"https:\/\/feellikelearning.com\/index.php\/2020\/04\/27\/leetcode-518-coin-change-2-solution\/","title":{"rendered":"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 518. Coin Change 2\u89e3\u6cd5"},"content":{"rendered":"\n<p>\u4f5c\u4e3alc322 coin change\u7684follow up\uff0cleetcode\u6807\u8bb0\u4e3amedium\uff0c\u800c\u4e14acceptance rate\u4e3a47%\uff0c\u9ad8\u4e8ecoin change\u768434.2%\u3002\u4e2a\u4eba\u505a\u4e0b\u6765\u89c9\u5f97\u5f88\u96be\uff0c\u6bd4322\u8981\u96be\u3002\u5199\u51fa\u4e00\u4e2a\u903b\u8f91\u6b63\u786e\u7684\u89e3\u6cd5\u8fd8\u7b97OK\uff0c\u4f46\u662fOJ\u4f1a\u8d85\u65f6\u3002<\/p>\n\n\n\n<p>Python \u7248\u672c top down dfs + backtrace<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution(object):\n    def change(self, amount, coins):\n        \"\"\"\n        :type amount: int\n        :type coins: List[int]\n        :rtype: int\n        \"\"\"\n        cur_path = []\n        paths = set()\n        self.dfs(amount, coins, cur_path, paths)\n        return len(paths)\n        \n    def dfs(self, amount, coins, cur_path, paths):\n        if amount == 0:\n            path = tuple(sorted(cur_path))\n            paths.add(path)\n            return\n        if amount &lt; 0:\n            return\n        for coin in coins:\n            cur_path.append(coin)\n            self.dfs(amount - coin, coins, cur_path, paths)\n            del cur_path[-1]\n        <\/code><\/pre>\n\n\n\n<p>Python \u7248\u672c bottom up dp<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution(object):\n    def change(self, amount, coins):\n        \"\"\"\n        :type amount: int\n        :type coins: List[int]\n        :rtype: int\n        \"\"\"\n        dp = [None] * (amount + 1)\n        dp[0] = set()\n        dp[0].add(())\n        for i in range(1, amount + 1):\n            combos = set()\n            for coin in coins:\n                rest = i - coin\n                if rest >= 0 and dp[rest] is not None:\n                    for rest_combo in dp[rest]:\n                        combo = tuple(sorted(rest_combo + (coin,)))\n                        combos.add(combo)\n            if len(combos) > 0:\n                dp[i] = combos\n                \n        return len(dp[amount]) if dp[amount] is not None else 0 <\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u4e24\u79cd\u90fd\u8d85\u65f6\u3002<\/p>\n\n\n\n<p>\u5b9e\u5728\u60f3\u4e0d\u5230\u80fd\u8fc7OJ\u7684\u7b97\u6cd5\uff0c\u4ee5\u4e0bdp\u7b97\u6cd5\u4e5f\u662f\u6211\u4ece\u7f51\u4e0a\u5927\u795e\u4eec\u90a3\u5b66\u5230\u4e86\u3002\u76ef\u7740\u770b\u4e862\u5929\u624d\u7b97\u60f3\u660e\u767d\u4e86\u5176\u4e2d\u9053\u7406\u3002\u5199\u5f97\u975e\u5e38\u7b80\u77ed\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution(object):\n    def change(self, amount, coins):\n        \"\"\"\n        :type amount: int\n        :type coins: List&#91;int]\n        :rtype: int\n        \"\"\"\n        dp = &#91;1] + &#91;0] * amount\n        for coin in coins:\n            for i in xrange(amount - coin + 1):\n                if dp&#91;i]:\n                    dp&#91;i + coin] += dp&#91;i]\n        return dp&#91;amount]<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u4e3alc322 coin change\u7684follow up\uff0cleetcode\u6807\u8bb0\u4e3amedium\uff0c\u800c\u4e14acceptance rate\u4e3a47%\uff0c\u9ad8\u4e8ecoin change\u768434.2%\u3002\u4e2a\u4eba\u505a\u4e0b\u6765\u89c9\u5f97\u5f88\u96be\uff0c\u6bd4322\u8981\u96be\u3002\u5199\u51fa\u4e00\u4e2a\u903b\u8f91\u6b63\u786e\u7684\u89e3\u6cd5\u8fd8\u7b97OK\uff0c\u4f46\u662fOJ\u4f1a\u8d85\u65f6\u3002 Python \u7248\u672c top down dfs + backtrace Python \u7248\u672c bottom up dp \u4ee5\u4e0a\u4e24\u79cd\u90fd\u8d85\u65f6\u3002 \u5b9e\u5728\u60f3\u4e0d\u5230\u80fd\u8fc7OJ\u7684\u7b97\u6cd5\uff0c\u4ee5\u4e0bdp\u7b97\u6cd5\u4e5f\u662f\u6211\u4ece\u7f51\u4e0a\u5927\u795e\u4eec\u90a3\u5b66\u5230\u4e86\u3002\u76ef\u7740\u770b\u4e862\u5929\u624d\u7b97\u60f3\u660e\u767d\u4e86\u5176\u4e2d\u9053\u7406\u3002\u5199\u5f97\u975e\u5e38\u7b80\u77ed\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[1],"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 518. Coin Change 2\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\/27\/leetcode-518-coin-change-2-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 518. Coin Change 2\u89e3\u6cd5 | Feel Like Learning\" \/>\n<meta property=\"og:description\" content=\"\u4f5c\u4e3alc322 coin change\u7684follow up\uff0cleetcode\u6807\u8bb0\u4e3amedium\uff0c\u800c\u4e14acceptance rate\u4e3a47%\uff0c\u9ad8\u4e8ecoin change\u768434.2%\u3002\u4e2a\u4eba\u505a\u4e0b\u6765\u89c9\u5f97\u5f88\u96be\uff0c\u6bd4322\u8981\u96be\u3002\u5199\u51fa\u4e00\u4e2a\u903b\u8f91\u6b63\u786e\u7684\u89e3\u6cd5\u8fd8\u7b97OK\uff0c\u4f46\u662fOJ\u4f1a\u8d85\u65f6\u3002 Python \u7248\u672c top down dfs + backtrace Python \u7248\u672c bottom up dp \u4ee5\u4e0a\u4e24\u79cd\u90fd\u8d85\u65f6\u3002 \u5b9e\u5728\u60f3\u4e0d\u5230\u80fd\u8fc7OJ\u7684\u7b97\u6cd5\uff0c\u4ee5\u4e0bdp\u7b97\u6cd5\u4e5f\u662f\u6211\u4ece\u7f51\u4e0a\u5927\u795e\u4eec\u90a3\u5b66\u5230\u4e86\u3002\u76ef\u7740\u770b\u4e862\u5929\u624d\u7b97\u60f3\u660e\u767d\u4e86\u5176\u4e2d\u9053\u7406\u3002\u5199\u5f97\u975e\u5e38\u7b80\u77ed\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/feellikelearning.com\/index.php\/2020\/04\/27\/leetcode-518-coin-change-2-solution\/?variant=zh-hant\" \/>\n<meta property=\"og:site_name\" content=\"Feel Like Learning\" \/>\n<meta property=\"article:published_time\" content=\"2020-04-27T00:47:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-04-29T08:12:58+00:00\" \/>\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=\"1 minute\" \/>\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\/27\/leetcode-518-coin-change-2-solution\/?variant=zh-hant#article\",\"isPartOf\":{\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/04\/27\/leetcode-518-coin-change-2-solution\/?variant=zh-hant\"},\"author\":{\"name\":\"feellikelearning\",\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\"},\"headline\":\"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 518. Coin Change 2\u89e3\u6cd5\",\"datePublished\":\"2020-04-27T00:47:22+00:00\",\"dateModified\":\"2020-04-29T08:12:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/04\/27\/leetcode-518-coin-change-2-solution\/?variant=zh-hant\"},\"wordCount\":27,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/feellikelearning.com\/index.php\/2020\/04\/27\/leetcode-518-coin-change-2-solution\/?variant=zh-hant#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/04\/27\/leetcode-518-coin-change-2-solution\/?variant=zh-hant\",\"url\":\"https:\/\/feellikelearning.com\/index.php\/2020\/04\/27\/leetcode-518-coin-change-2-solution\/?variant=zh-hant\",\"name\":\"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 518. Coin Change 2\u89e3\u6cd5 | Feel Like Learning\",\"isPartOf\":{\"@id\":\"https:\/\/feellikelearning.com\/#website\"},\"datePublished\":\"2020-04-27T00:47:22+00:00\",\"dateModified\":\"2020-04-29T08:12:58+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/04\/27\/leetcode-518-coin-change-2-solution\/?variant=zh-hant#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/feellikelearning.com\/index.php\/2020\/04\/27\/leetcode-518-coin-change-2-solution\/?variant=zh-hant\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/04\/27\/leetcode-518-coin-change-2-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 518. Coin Change 2\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 518. Coin Change 2\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\/27\/leetcode-518-coin-change-2-solution\/?variant=zh-hant","og_locale":"en_US","og_type":"article","og_title":"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 518. Coin Change 2\u89e3\u6cd5 | Feel Like Learning","og_description":"\u4f5c\u4e3alc322 coin change\u7684follow up\uff0cleetcode\u6807\u8bb0\u4e3amedium\uff0c\u800c\u4e14acceptance rate\u4e3a47%\uff0c\u9ad8\u4e8ecoin change\u768434.2%\u3002\u4e2a\u4eba\u505a\u4e0b\u6765\u89c9\u5f97\u5f88\u96be\uff0c\u6bd4322\u8981\u96be\u3002\u5199\u51fa\u4e00\u4e2a\u903b\u8f91\u6b63\u786e\u7684\u89e3\u6cd5\u8fd8\u7b97OK\uff0c\u4f46\u662fOJ\u4f1a\u8d85\u65f6\u3002 Python \u7248\u672c top down dfs + backtrace Python \u7248\u672c bottom up dp \u4ee5\u4e0a\u4e24\u79cd\u90fd\u8d85\u65f6\u3002 \u5b9e\u5728\u60f3\u4e0d\u5230\u80fd\u8fc7OJ\u7684\u7b97\u6cd5\uff0c\u4ee5\u4e0bdp\u7b97\u6cd5\u4e5f\u662f\u6211\u4ece\u7f51\u4e0a\u5927\u795e\u4eec\u90a3\u5b66\u5230\u4e86\u3002\u76ef\u7740\u770b\u4e862\u5929\u624d\u7b97\u60f3\u660e\u767d\u4e86\u5176\u4e2d\u9053\u7406\u3002\u5199\u5f97\u975e\u5e38\u7b80\u77ed\u3002","og_url":"https:\/\/feellikelearning.com\/index.php\/2020\/04\/27\/leetcode-518-coin-change-2-solution\/?variant=zh-hant","og_site_name":"Feel Like Learning","article_published_time":"2020-04-27T00:47:22+00:00","article_modified_time":"2020-04-29T08:12:58+00:00","author":"feellikelearning","twitter_card":"summary_large_image","twitter_misc":{"Written by":"feellikelearning","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/feellikelearning.com\/index.php\/2020\/04\/27\/leetcode-518-coin-change-2-solution\/?variant=zh-hant#article","isPartOf":{"@id":"https:\/\/feellikelearning.com\/index.php\/2020\/04\/27\/leetcode-518-coin-change-2-solution\/?variant=zh-hant"},"author":{"name":"feellikelearning","@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a"},"headline":"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 518. Coin Change 2\u89e3\u6cd5","datePublished":"2020-04-27T00:47:22+00:00","dateModified":"2020-04-29T08:12:58+00:00","mainEntityOfPage":{"@id":"https:\/\/feellikelearning.com\/index.php\/2020\/04\/27\/leetcode-518-coin-change-2-solution\/?variant=zh-hant"},"wordCount":27,"commentCount":0,"publisher":{"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a"},"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/feellikelearning.com\/index.php\/2020\/04\/27\/leetcode-518-coin-change-2-solution\/?variant=zh-hant#respond"]}]},{"@type":"WebPage","@id":"https:\/\/feellikelearning.com\/index.php\/2020\/04\/27\/leetcode-518-coin-change-2-solution\/?variant=zh-hant","url":"https:\/\/feellikelearning.com\/index.php\/2020\/04\/27\/leetcode-518-coin-change-2-solution\/?variant=zh-hant","name":"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 518. Coin Change 2\u89e3\u6cd5 | Feel Like Learning","isPartOf":{"@id":"https:\/\/feellikelearning.com\/#website"},"datePublished":"2020-04-27T00:47:22+00:00","dateModified":"2020-04-29T08:12:58+00:00","breadcrumb":{"@id":"https:\/\/feellikelearning.com\/index.php\/2020\/04\/27\/leetcode-518-coin-change-2-solution\/?variant=zh-hant#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/feellikelearning.com\/index.php\/2020\/04\/27\/leetcode-518-coin-change-2-solution\/?variant=zh-hant"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/feellikelearning.com\/index.php\/2020\/04\/27\/leetcode-518-coin-change-2-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 518. Coin Change 2\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\/192"}],"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=192"}],"version-history":[{"count":3,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts\/192\/revisions"}],"predecessor-version":[{"id":202,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts\/192\/revisions\/202"}],"wp:attachment":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/media?parent=192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/categories?post=192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/tags?post=192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}