{"id":1033,"date":"2025-12-24T05:31:34","date_gmt":"2025-12-23T21:31:34","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=1033"},"modified":"2025-12-24T05:31:34","modified_gmt":"2025-12-23T21:31:34","slug":"leetcode-322-%e9%9b%b6%e9%92%b1%e5%85%91%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=1033","title":{"rendered":"LeetCode 322 \u2013 \u96f6\u94b1\u5151\u6362"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 coins \uff0c\u8868\u793a\u4e0d\u540c\u9762\u989d\u7684\u786c\u5e01\uff1b\u4ee5\u53ca\u4e00\u4e2a\u6574\u6570 amount \uff0c\u8868\u793a\u603b\u91d1\u989d\u3002<br \/>\n\u8ba1\u7b97\u5e76\u8fd4\u56de\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 -1 \u3002<br \/>\n\u4f60\u53ef\u4ee5\u8ba4\u4e3a\u6bcf\u79cd\u786c\u5e01\u7684\u6570\u91cf\u662f\u65e0\u9650\u7684\u3002<\/p>\n<p>\u793a\u4f8b\uff1a<br \/>\n\u8f93\u5165\uff1acoins = [1, 2, 5], amount = 11<br \/>\n\u8f93\u51fa\uff1a3<br \/>\n\u89e3\u91ca\uff1a11 = 5 + 5 + 1<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<p>\u52a8\u6001\u89c4\u5212<br \/>\n\u8bbe ans[i] \u4e3a\u7ec4\u6210\u603b\u91d1\u989d i \u7684\u6700\u5c11\u786c\u5e01\u6570\u91cf\uff0c\u5219 ans[i] =  ans[i &#8211; \u67d0\u4e2a\u786c\u5e01\u9762\u989d] + 1\uff0c\u679a\u4e3e\u6240\u6709\u9762\u989d\u540e\u53d6\u6700\u5c0f\u503c\u3002<\/p>\n<p>\u65f6\u95f4\u590d\u6742\u5ea6 O(m * n)\uff0cm\u4e3a\u786c\u5e01\u79cd\u7c7b\uff0cn\u4e3a\u603b\u91d1\u989d<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">public int coinChange(int[] coins, int amount) {\n    int MAX = amount + 1;\n    int[] ans = new int[amount + 1];\n    for (int i = 1; i &lt;= amount; i++) {\n        ans[i] = MAX;\n        for (int coin : coins) {\n            if (coin &lt;= i) {\n                int pre = ans[i - coin];\n                ans[i] = Math.min(ans[i], pre + 1);\n            }\n        }\n    }\n    return ans[amount] == MAX ? -1 : ans[amount];\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">fun coinChange(coins: IntArray, amount: Int): Int {\n    val ans = IntArray(amount + 1)\n    val MAX = amount + 1\n    for (i in 1..amount) {\n        ans[i] = MAX\n        for (c in coins) {\n            if (c &lt;= i) {\n                ans[i] = min(ans[i], ans[i - c] + 1)\n            }\n        }\n    }\n    return if (ans[amount] == MAX) -1 else ans[amount]\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 coins \uff0c\u8868\u793a\u4e0d\u540c\u9762\u989d\u7684\u786c\u5e01\uff1b\u4ee5\u53ca\u4e00\u4e2a\u6574\u6570 amount \uff0c\u8868\u793a\u603b\u91d1\u989d\u3002 \u8ba1\u7b97 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[20,40],"class_list":["post-1033","post","type-post","status-publish","format-standard","hentry","category-algo","tag-leetcode","tag-40"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1033","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1033"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1033\/revisions"}],"predecessor-version":[{"id":1034,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1033\/revisions\/1034"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}