{"id":1031,"date":"2025-12-24T05:00:40","date_gmt":"2025-12-23T21:00:40","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=1031"},"modified":"2025-12-24T05:00:40","modified_gmt":"2025-12-23T21:00:40","slug":"leetcode-279-%e5%ae%8c%e5%85%a8%e5%b9%b3%e6%96%b9%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=1031","title":{"rendered":"LeetCode 279 \u2013 \u5b8c\u5168\u5e73\u65b9\u6570"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570 n \uff0c\u8fd4\u56de \u548c\u4e3a n \u7684\u5b8c\u5168\u5e73\u65b9\u6570\u7684\u6700\u5c11\u6570\u91cf \u3002<br \/>\n\u5b8c\u5168\u5e73\u65b9\u6570 \u662f\u4e00\u4e2a\u6574\u6570\uff0c\u5176\u503c\u7b49\u4e8e\u53e6\u4e00\u4e2a\u6574\u6570\u7684\u5e73\u65b9\uff1b\u6362\u53e5\u8bdd\u8bf4\uff0c\u5176\u503c\u7b49\u4e8e\u4e00\u4e2a\u6574\u6570\u81ea\u4e58\u7684\u79ef\u3002\u4f8b\u5982\uff0c1\u30014\u30019 \u548c 16 \u90fd\u662f\u5b8c\u5168\u5e73\u65b9\u6570\uff0c\u800c 3 \u548c 11 \u4e0d\u662f\u3002<\/p>\n<p>\u793a\u4f8b\uff1a<br \/>\n\u8f93\u5165\uff1an = 12<br \/>\n\u8f93\u51fa\uff1a3<br \/>\n\u89e3\u91ca\uff1a12 = 4 + 4 + 4<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<p>\u52a8\u6001\u89c4\u5212\uff0cans[n] = ans[n &#8211; \u5b8c\u5168\u5e73\u65b9\u6570] + 1<br \/>\n\u9012\u63a8\u7684\u540c\u65f6\uff0c\u679a\u4e3e\u5b8c\u5168\u5e73\u65b9\u6570\u5373\u53ef\u3002<\/p>\n<p>\u65f6\u95f4\u590d\u6742\u5ea6 O(n * sqrt(n))<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">public int numSquares(int n) {\n    int[] ans = new int[n + 1];\n    for (int i = 1; i &lt;= n; i++) {\n        ans[i] = Integer.MAX_VALUE;\n        for (int j = 1; j * j &lt;= i; j++) {\n            ans[i] = Math.min(ans[i], ans[i - j * j] + 1);\n        }\n    }\n    return ans[n];\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">fun numSquares(n: Int): Int {\n    val ans = IntArray(n + 1) { Int.MAX_VALUE }\n    ans[0] = 0\n    for (i in 1..n) {\n        for (j in 1..i) {\n            val square = j * j\n            if (square &lt;= i) {\n                ans[i] = min(ans[i], ans[i - square] + 1)\n            } else {\n                break\n            }\n        }\n    }\n    return ans[n]\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u7ed9\u4f60\u4e00\u4e2a\u6574\u6570 n \uff0c\u8fd4\u56de \u548c\u4e3a n \u7684\u5b8c\u5168\u5e73\u65b9\u6570\u7684\u6700\u5c11\u6570\u91cf \u3002 \u5b8c\u5168\u5e73\u65b9\u6570 \u662f\u4e00\u4e2a\u6574\u6570\uff0c\u5176\u503c\u7b49\u4e8e\u53e6\u4e00 [&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-1031","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\/1031","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=1031"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1031\/revisions"}],"predecessor-version":[{"id":1032,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1031\/revisions\/1032"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}