{"id":351,"date":"2020-09-26T10:29:53","date_gmt":"2020-09-26T02:29:53","guid":{"rendered":"http:\/\/39.96.58.60\/?p=351"},"modified":"2022-10-18T16:39:49","modified_gmt":"2022-10-18T08:39:49","slug":"leetcode-322-%e9%9b%b6%e9%92%b1%e5%85%91%e6%8d%a2","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=351","title":{"rendered":"Leetcode 322. \u96f6\u94b1\u5151\u6362"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e0d\u540c\u9762\u989d\u7684\u786c\u5e01 coins \u548c\u4e00\u4e2a\u603b\u91d1\u989d amount\u3002\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u8ba1\u7b97\u53ef\u4ee5\u51d1\u6210\u603b\u91d1\u989d\u6240\u9700\u7684\u6700\u5c11\u7684\u786c\u5e01\u4e2a\u6570\u3002\u5982\u679c\u6ca1\u6709\u4efb\u4f55\u4e00\u79cd\u786c\u5e01\u7ec4\u5408\u80fd\u7ec4\u6210\u603b\u91d1\u989d\uff0c\u8fd4\u56de&nbsp;<code>-1<\/code>\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b&nbsp;1:<\/strong><\/p>\n<pre><strong>\u8f93\u5165: <\/strong>coins = <code>[1, 2, 5]<\/code>, amount = <code>11<\/code>\n\n<strong>\u8f93\u51fa: <\/strong><code>3<\/code> \n\n<strong>\u89e3\u91ca:<\/strong> 11 = 5 + 5 + 1<\/pre>\n<p><strong>\u793a\u4f8b 2:<\/strong><\/p>\n<pre><strong>\u8f93\u5165: <\/strong>coins = <code>[2]<\/code>, amount = <code>3<\/code>\n\n<strong>\u8f93\u51fa: <\/strong>-1<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u8bf4\u660e<\/strong>:<\/p>\n<p>\u4f60\u53ef\u4ee5\u8ba4\u4e3a\u6bcf\u79cd\u786c\u5e01\u7684\u6570\u91cf\u662f\u65e0\u9650\u7684\u3002<\/p>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u52a8\u6001\u89c4\u5212\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a1668 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8645.86% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.8 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8667.60% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n\n\"\"\"\nclass Solution:\n    def coinChange(self, coins: List[int], amount: int) -&gt; int:\n        dp = [-1 for _ in range(amount+1)]\n        dp[0] = 0\n        for i in range(1, amount+1):\n            record = []\n            for coin in coins:\n                if i - coin == 0:\n                    dp[i] = 1\n                    break\n                if i - coin &gt; 0 and dp[i-coin] &gt;= 0:\n                    record.append(dp[i-coin])\n            if record != [] and dp[i] != 1:\n                dp[i] = min(record) + 1\n        return dp[-1]\n\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a1556 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8662.00% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.9 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8628.90% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u7cbe\u7b80\u4e0b\u4ee3\u7801,\u5e76\u5c06\u4e24\u6b21\u5faa\u73af\u7684\u987a\u5e8f\u8c03\u6362\u4e86\u4e0b\uff0c\u7b2c\u4e8c\u6b21\u5faa\u73af\u4ece\u5f53\u524d\u786c\u5e01\u5e01\u503c\u5f00\u59cb\n\"\"\"\nclass Solution:\n    def coinChange(self, coins: List[int], amount: int) -&gt; int:\n        dp = [float('inf') for _ in range(amount+1)]\n        dp[0] = 0\n        for coin in coins:\n            for i in range(coin, amount+1):\n                dp[i] = min(dp[i], dp[i-coin] + 1)\n        return dp[-1] if dp[-1] != float('inf') else -1<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e0d\u540c\u9762\u989d\u7684\u786c\u5e01 coins \u548c\u4e00\u4e2a\u603b\u91d1\u989d amount\u3002\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u8ba1\u7b97\u53ef\u4ee5\u51d1\u6210\u603b\u91d1\u989d\u6240\u9700\u7684\u6700\u5c11\u7684\u786c\u5e01\u4e2a&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/351"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=351"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/351\/revisions"}],"predecessor-version":[{"id":352,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/351\/revisions\/352"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=351"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}