{"id":1095949,"date":"2025-01-08T14:56:23","date_gmt":"2025-01-08T06:56:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1095949.html"},"modified":"2025-01-08T14:56:25","modified_gmt":"2025-01-08T06:56:25","slug":"python%e5%a6%82%e4%bd%95%e5%81%9a%e6%9c%80%e5%b0%91%e7%a1%ac%e5%b8%81%e6%89%be%e9%9b%b6-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1095949.html","title":{"rendered":"python\u5982\u4f55\u505a\u6700\u5c11\u786c\u5e01\u627e\u96f6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211356\/b2459471-7c3a-4c0c-a338-5c99af0bc6e4.webp\" alt=\"python\u5982\u4f55\u505a\u6700\u5c11\u786c\u5e01\u627e\u96f6\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u5b9e\u73b0\u6700\u5c11\u786c\u5e01\u627e\u96f6\u95ee\u9898\u53ef\u4ee5\u901a\u8fc7\u52a8\u6001\u89c4\u5212\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<strong>\u6700\u5c11\u786c\u5e01\u627e\u96f6\u95ee\u9898\u53ef\u4ee5\u901a\u8fc7\u52a8\u6001\u89c4\u5212\u3001\u8d2a\u5fc3\u7b97\u6cd5\u6765\u89e3\u51b3<\/strong>\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u52a8\u6001\u89c4\u5212\u6765\u5b9e\u73b0\u8fd9\u4e00\u7b97\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u52a8\u6001\u89c4\u5212\u65b9\u6cd5\u7684\u6838\u5fc3\u601d\u60f3\u662f\uff1a\u8bbe\u5b9a\u4e00\u4e2a\u6570\u7ec4\u6765\u4fdd\u5b58\u6bcf\u4e2a\u91d1\u989d\u6240\u9700\u7684\u6700\u5c11\u786c\u5e01\u6570\u91cf\uff0c\u4ece\u6700\u5c0f\u91d1\u989d\u9010\u6b65\u8ba1\u7b97\u5230\u76ee\u6807\u91d1\u989d\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u95ee\u9898\u5b9a\u4e49\u4e0e\u8f93\u5165\u8f93\u51fa<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b9a\u4e49\u6700\u5c11\u786c\u5e01\u627e\u96f6\u95ee\u9898\u7684\u8f93\u5165\u548c\u8f93\u51fa\u3002\u8f93\u5165\u5305\u62ec\u53ef\u7528\u786c\u5e01\u7684\u9762\u989d\u5217\u8868\u548c\u76ee\u6807\u91d1\u989d\uff0c\u8f93\u51fa\u662f\u7ec4\u6210\u76ee\u6807\u91d1\u989d\u6240\u9700\u7684\u6700\u5c11\u786c\u5e01\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def coin_change(coins, amount):<\/p>\n<p>    # \u521b\u5efa\u4e00\u4e2a\u6570\u7ec4 dp\uff0c\u5176\u4e2d dp[i] \u8868\u793a\u7ec4\u6210\u91d1\u989d i \u6240\u9700\u7684\u6700\u5c11\u786c\u5e01\u6570\u91cf<\/p>\n<p>    dp = [float(&#39;inf&#39;)] * (amount + 1)<\/p>\n<p>    dp[0] = 0  # \u7ec4\u6210\u91d1\u989d 0 \u6240\u9700\u7684\u786c\u5e01\u6570\u91cf\u4e3a 0<\/p>\n<p>    # \u904d\u5386\u6bcf\u4e00\u4e2a\u91d1\u989d\uff0c\u4ece 1 \u5230 amount<\/p>\n<p>    for i in range(1, amount + 1):<\/p>\n<p>        for coin in coins:<\/p>\n<p>            if i - coin &gt;= 0:<\/p>\n<p>                dp[i] = min(dp[i], dp[i - coin] + 1)<\/p>\n<p>    return dp[amount] if dp[amount] != float(&#39;inf&#39;) else -1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u52a8\u6001\u89c4\u5212\u89e3\u51b3\u65b9\u6848<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521d\u59cb\u5316\u6570\u7ec4\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u6570\u7ec4 <code>dp<\/code>\uff0c\u5176\u4e2d <code>dp[i]<\/code> \u8868\u793a\u7ec4\u6210\u91d1\u989d <code>i<\/code> \u6240\u9700\u7684\u6700\u5c11\u786c\u5e01\u6570\u91cf\u3002\u521d\u59cb\u5316\u65f6\uff0c\u5c06 <code>dp[0]<\/code> \u8bbe\u4e3a 0\uff0c\u56e0\u4e3a\u7ec4\u6210\u91d1\u989d 0 \u6240\u9700\u7684\u786c\u5e01\u6570\u91cf\u4e3a 0\u3002\u5176\u4f59\u4f4d\u7f6e\u521d\u59cb\u5316\u4e3a\u6b63\u65e0\u7a77\u5927\uff0c\u8868\u793a\u6682\u65f6\u65e0\u6cd5\u7ec4\u6210\u5bf9\u5e94\u7684\u91d1\u989d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u904d\u5386\u91d1\u989d\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u4ece 1 \u5230 <code>amount<\/code> \u904d\u5386\u6bcf\u4e00\u4e2a\u91d1\u989d <code>i<\/code>\uff0c\u5bf9\u4e8e\u6bcf\u4e00\u4e2a\u91d1\u989d\uff0c\u904d\u5386\u6240\u6709\u7684\u786c\u5e01\u9762\u989d\uff0c\u68c0\u67e5\u786c\u5e01\u662f\u5426\u80fd\u7528\u4e8e\u5f53\u524d\u91d1\u989d\u3002\u5982\u679c\u80fd\uff0c\u5219\u66f4\u65b0 <code>dp[i]<\/code> \u4e3a\u5f53\u524d\u503c\u548c <code>dp[i - coin] + 1<\/code> \u7684\u6700\u5c0f\u503c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd4\u56de\u7ed3\u679c\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u68c0\u67e5 <code>dp[amount]<\/code> \u662f\u5426\u4ecd\u4e3a\u6b63\u65e0\u7a77\u5927\u3002\u5982\u679c\u662f\uff0c\u5219\u8868\u793a\u65e0\u6cd5\u7ec4\u6210\u76ee\u6807\u91d1\u989d\uff0c\u8fd4\u56de -1\uff1b\u5426\u5219\u8fd4\u56de <code>dp[amount]<\/code>\uff0c\u5373\u6700\u5c11\u786c\u5e01\u6570\u91cf\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u793a\u4f8b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">coins = [1, 2, 5]<\/p>\n<p>amount = 11<\/p>\n<p>result = coin_change(coins, amount)<\/p>\n<p>print(f&quot;The minimum number of coins needed: {result}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6709\u786c\u5e01\u9762\u989d\u4e3a 1\u30012 \u548c 5 \u7684\u786c\u5e01\uff0c\u76ee\u6807\u91d1\u989d\u4e3a 11\u3002\u901a\u8fc7\u8c03\u7528 <code>coin_change<\/code> \u51fd\u6570\uff0c\u53ef\u4ee5\u5f97\u5230\u7ec4\u6210 11 \u6240\u9700\u7684\u6700\u5c11\u786c\u5e01\u6570\u91cf\u4e3a 3\uff085 + 5 + 1\uff09\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u65f6\u95f4\u590d\u6742\u5ea6\u4e0e\u7a7a\u95f4\u590d\u6742\u5ea6<\/h3>\n<\/p>\n<p><p>\u52a8\u6001\u89c4\u5212\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a <code>O(n * m)<\/code>\uff0c\u5176\u4e2d <code>n<\/code> \u4e3a\u76ee\u6807\u91d1\u989d\uff0c<code>m<\/code> \u4e3a\u786c\u5e01\u7684\u79cd\u7c7b\u6570\u3002\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a <code>O(n)<\/code>\uff0c\u56e0\u4e3a\u9700\u8981\u4e00\u4e2a\u957f\u5ea6\u4e3a <code>amount + 1<\/code> \u7684\u6570\u7ec4\u6765\u5b58\u50a8\u6bcf\u4e2a\u91d1\u989d\u6240\u9700\u7684\u6700\u5c11\u786c\u5e01\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u8d2a\u5fc3\u7b97\u6cd5\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u7279\u5b9a\u60c5\u51b5\u4e0b\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u4e5f\u80fd\u89e3\u51b3\u6700\u5c11\u786c\u5e01\u627e\u96f6\u95ee\u9898\u3002\u8d2a\u5fc3\u7b97\u6cd5\u7684\u6838\u5fc3\u601d\u60f3\u662f\uff1a\u6bcf\u6b21\u9009\u62e9\u9762\u503c\u6700\u5927\u7684\u786c\u5e01\uff0c\u76f4\u5230\u65e0\u6cd5\u9009\u62e9\u4e3a\u6b62\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def coin_change_greedy(coins, amount):<\/p>\n<p>    coins.sort(reverse=True)<\/p>\n<p>    count = 0<\/p>\n<p>    for coin in coins:<\/p>\n<p>        if amount == 0:<\/p>\n<p>            break<\/p>\n<p>        count += amount \/\/ coin<\/p>\n<p>        amount %= coin<\/p>\n<p>    return count if amount == 0 else -1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u6bd4\u8f83\u52a8\u6001\u89c4\u5212\u4e0e\u8d2a\u5fc3\u7b97\u6cd5<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9002\u7528\u6027\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u52a8\u6001\u89c4\u5212\u9002\u7528\u4e8e\u6240\u6709\u60c5\u51b5\uff0c\u4f46\u65f6\u95f4\u590d\u6742\u5ea6\u8f83\u9ad8\u3002\u8d2a\u5fc3\u7b97\u6cd5\u9002\u7528\u4e8e\u7279\u5b9a\u60c5\u51b5\uff0c\u5f53\u786c\u5e01\u9762\u989d\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\u65f6\uff08\u5982\u6240\u6709\u786c\u5e01\u9762\u989d\u4e92\u8d28\uff09\uff0c\u80fd\u5feb\u901f\u5f97\u5230\u7ed3\u679c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u590d\u6742\u5ea6\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u52a8\u6001\u89c4\u5212\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a <code>O(n * m)<\/code>\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a <code>O(m log m)<\/code>\uff08\u6392\u5e8f\uff09\u6216 <code>O(m)<\/code>\uff08\u5982\u679c\u786c\u5e01\u9762\u989d\u5df2\u6392\u5e8f\uff09\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u6700\u5c11\u786c\u5e01\u627e\u96f6\u95ee\u9898\u53ef\u4ee5\u901a\u8fc7\u52a8\u6001\u89c4\u5212\u3001\u8d2a\u5fc3\u7b97\u6cd5\u6765\u89e3\u51b3\u3002<\/strong> \u52a8\u6001\u89c4\u5212\u65b9\u6cd5\u901a\u7528\u6027\u5f3a\uff0c\u9002\u7528\u4e8e\u6240\u6709\u60c5\u51b5\uff0c\u4f46\u65f6\u95f4\u590d\u6742\u5ea6\u8f83\u9ad8\u3002\u8d2a\u5fc3\u7b97\u6cd5\u9002\u7528\u4e8e\u7279\u5b9a\u60c5\u51b5\uff0c\u80fd\u5feb\u901f\u5f97\u5230\u7ed3\u679c\u3002\u5177\u4f53\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5b9e\u9645\u95ee\u9898\u7684\u8981\u6c42\u548c\u786c\u5e01\u9762\u989d\u7684\u7279\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\u548c\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u6700\u5c11\u786c\u5e01\u627e\u96f6\u95ee\u9898\uff0c\u5e76\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u6700\u5c11\u786c\u5e01\u627e\u96f6\u7684\u7b97\u6cd5\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u52a8\u6001\u89c4\u5212\u7b97\u6cd5\u6765\u89e3\u51b3\u6700\u5c11\u786c\u5e01\u627e\u96f6\u7684\u95ee\u9898\u3002\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u6570\u7ec4\u6765\u5b58\u50a8\u4ece0\u5230\u76ee\u6807\u91d1\u989d\u7684\u6700\u5c11\u786c\u5e01\u6570\u91cf\uff0c\u9010\u6b65\u66f4\u65b0\u6bcf\u4e2a\u91d1\u989d\u6240\u9700\u7684\u6700\u5c0f\u786c\u5e01\u6570\uff0c\u6700\u7ec8\u5f97\u5230\u76ee\u6807\u91d1\u989d\u7684\u89e3\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"language-python\">def coin_change(coins, amount):\n    dp = [float(&#39;inf&#39;)] * (amount + 1)\n    dp[0] = 0\n\n    for coin in coins:\n        for x in range(coin, amount + 1):\n            dp[x] = min(dp[x], dp[x - coin] + 1)\n\n    return dp[amount] if dp[amount] != float(&#39;inf&#39;) else -1\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528\u52a8\u6001\u89c4\u5212\u7684\u590d\u6742\u5ea6\u5982\u4f55\uff1f<\/strong><br \/>\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n*m)\uff0c\u5176\u4e2dn\u662f\u76ee\u6807\u91d1\u989d\uff0cm\u662f\u786c\u5e01\u7684\u79cd\u7c7b\u6570\u3002\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3aO(n)\uff0c\u56e0\u4e3a\u9700\u8981\u4e00\u4e2a\u6570\u7ec4\u6765\u5b58\u50a8\u6bcf\u4e2a\u91d1\u989d\u6240\u9700\u7684\u6700\u5c0f\u786c\u5e01\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u975e\u5e38\u9ad8\u6548\uff0c\u9002\u5408\u89e3\u51b3\u8f83\u5c0f\u7684\u91d1\u989d\u548c\u786c\u5e01\u79cd\u7c7b\u7684\u95ee\u9898\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u7b56\u7565\u6765\u4f18\u5316\u786c\u5e01\u627e\u96f6\u7b97\u6cd5\uff1f<\/strong><br \/>\u53ef\u4ee5\u8003\u8651\u8d2a\u5fc3\u7b97\u6cd5\uff0c\u7279\u522b\u662f\u5728\u786c\u5e01\u9762\u989d\u4e3a\u7279\u5b9a\u503c\u7684\u60c5\u51b5\u4e0b\uff08\u59821, 5, 10, 25\u7b49\uff09\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u80fd\u591f\u5feb\u901f\u627e\u51fa\u89e3\u3002\u7136\u800c\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u5e76\u4e0d\u9002\u7528\u4e8e\u6240\u6709\u7684\u786c\u5e01\u7ec4\u5408\uff0c\u56e0\u6b64\u5728\u5b9e\u73b0\u65f6\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5\u3002\u5982\u679c\u786c\u5e01\u79cd\u7c7b\u8f83\u591a\u4e14\u9762\u989d\u4e0d\u5747\u5300\uff0c\u52a8\u6001\u89c4\u5212\u4ecd\u7136\u662f\u66f4\u5b89\u5168\u7684\u9009\u62e9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b9e\u73b0\u6700\u5c11\u786c\u5e01\u627e\u96f6\u95ee\u9898\u53ef\u4ee5\u901a\u8fc7\u52a8\u6001\u89c4\u5212\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u6700\u5c11\u786c\u5e01\u627e\u96f6\u95ee\u9898\u53ef\u4ee5\u901a\u8fc7\u52a8\u6001\u89c4\u5212\u3001\u8d2a\u5fc3\u7b97\u6cd5\u6765 [&hellip;]","protected":false},"author":3,"featured_media":1095955,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095949"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1095949"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095949\/revisions"}],"predecessor-version":[{"id":1095960,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095949\/revisions\/1095960"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1095955"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1095949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1095949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1095949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}