{"id":1047,"date":"2025-12-25T06:50:25","date_gmt":"2025-12-24T22:50:25","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=1047"},"modified":"2025-12-25T06:50:25","modified_gmt":"2025-12-24T22:50:25","slug":"leetcode-64-%e6%9c%80%e5%b0%8f%e8%b7%af%e5%be%84%e5%92%8c","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=1047","title":{"rendered":"LeetCode 64 \u2013 \u6700\u5c0f\u8def\u5f84\u548c"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u5305\u542b\u975e\u8d1f\u6574\u6570\u7684 m x n \u7f51\u683c grid \uff0c\u8bf7\u627e\u51fa\u4e00\u6761\u4ece\u5de6\u4e0a\u89d2\u5230\u53f3\u4e0b\u89d2\u7684\u8def\u5f84\uff0c\u4f7f\u5f97\u8def\u5f84\u4e0a\u7684\u6570\u5b57\u603b\u548c\u4e3a\u6700\u5c0f\u3002<br \/>\n\u8bf4\u660e\uff1a\u6bcf\u6b21\u53ea\u80fd\u5411\u4e0b\u6216\u8005\u5411\u53f3\u79fb\u52a8\u4e00\u6b65\u3002<\/p>\n<p>\u793a\u4f8b\uff1a<br \/>\n\u8f93\u5165\uff1agrid = [[1,3,1],[1,5,1],[4,2,1]]<br \/>\n\u8f93\u51fa\uff1a7<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<p>\u52a8\u6001\u89c4\u5212<br \/>\n\u8bbe dp[i][j] \u8868\u793a\u79fb\u52a8\u81f3 i, j \u6240\u9700\u7684\u6700\u5c0f\u603b\u548c\uff0c\u5219 <code>dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + grid[i][j]<\/code>\u3002<br \/>\n\u53ef\u4ee5\u5c06\u7a7a\u95f4\u4f18\u5316\u4e3a\u4e00\u7ef4\uff0c <code>dp[j] = min(dp[j], dp[j-1]) + grid[i][j]<\/code>\u3002<\/p>\n<p>\u65f6\u95f4\u590d\u6742\u5ea6 O(m * n)<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">public int minPathSum(int[][] grid) {\n    int m = grid.length, n = grid[0].length;\n    int[] dp = new int[n];\n    dp[0] = grid[0][0];\n    for (int i = 0; i &lt; m; i++) {\n        for (int j = 0; j &lt; n; j++) {\n            if (i == 0 &amp;&amp; j == 0) {\n                continue;\n            }\n            int left = j &gt;= 1 ? dp[j - 1] : Integer.MAX_VALUE;\n            int up = i &gt;= 1 ? dp[j] : Integer.MAX_VALUE;\n            dp[j] = Math.min(left, up) + grid[i][j];\n        }\n    }\n    return dp[n - 1];\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">fun minPathSum(grid: Array&lt;IntArray&gt;): Int {\n    val m = grid.size\n    val n = grid[0].size\n    val dp = IntArray(n)\n    dp[0] = grid[0][0]\n    for (i in 0 until m) {\n        for (j in 0 until n) {\n            if (i == 0 &amp;&amp; j == 0) continue\n            dp[j] = min(if (i &gt;= 1) dp[j] else Int.MAX_VALUE, if (j &gt;= 1) dp[j - 1] else Int.MAX_VALUE) + grid[i][j]\n        }\n    }\n    return dp[n - 1]\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u7ed9\u5b9a\u4e00\u4e2a\u5305\u542b\u975e\u8d1f\u6574\u6570\u7684 m x n \u7f51\u683c grid \uff0c\u8bf7\u627e\u51fa\u4e00\u6761\u4ece\u5de6\u4e0a\u89d2\u5230\u53f3\u4e0b\u89d2\u7684\u8def\u5f84\uff0c\u4f7f\u5f97\u8def\u5f84\u4e0a\u7684 [&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-1047","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\/1047","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=1047"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1047\/revisions"}],"predecessor-version":[{"id":1048,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1047\/revisions\/1048"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}