{"id":3017,"date":"2020-02-23T12:32:56","date_gmt":"2020-02-23T04:32:56","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=3017"},"modified":"2020-02-23T12:32:56","modified_gmt":"2020-02-23T04:32:56","slug":"%e3%80%90leetcode%e3%80%910064-%e6%9c%80%e5%b0%8f%e8%b7%af%e5%be%84%e5%92%8c","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/algorithm\/3017\/","title":{"rendered":"\u3010LeetCode\u30110064 \u6700\u5c0f\u8def\u5f84\u548c"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/minimum-path-sum\/\">https:\/\/leetcode-cn.com\/problems\/minimum-path-sum\/<\/a><\/p>\n\n\n<pre class=\"wp-block-preformatted\"># -*- coding:utf-8 -*-\n<em>\n<\/em>class Solution(object):\n    def minPathSum(self, grid):\n        <em>\"\"\"\n        <\/em><strong><em>:type<\/em><\/strong><em> grid: List[List[int]]\n        <\/em><strong><em>:rtype<\/em><\/strong><em>: int\n        \"\"\"\n        <\/em>dp = [0] * (len(grid[0]) + 1)\n        for i in range(len(grid[0])):\n            dp[i] = grid[0][i] + dp[i - 1]\n        dp[-1] = float('inf')\n        for i in range(1, len(grid)):\n            for j in range(len(grid[0])):\n                dp[j] = min(dp[j - 1] + grid[i][j], dp[j] + grid[i][j])\n        return dp[-2]\nif __name__ == '__main__':\n    grid = [\n        [1, 3, 1],\n        [1, 5, 1],\n        [4, 2, 1]\n    ]\n    grid = [[1, 2, 5], [3, 2, 1]]\n    print(Solution().minPathSum(grid))\n<\/pre>\n\n\n<p><strong>\u601d\u8def<\/strong>\uff1a\u6211\u4e4b\u524d\u505a\u8fc7\u65f6\u7a7a\u590d\u6742\u5ea6\u90fd\u662f90\u591a\u7684\uff0c\u4f46\u662f\u8fd9\u9053\u9898\u65f6\u7a7a\u590d\u6742\u5ea6\u90fd\u63a5\u8fd1100\uff0c\u592a\u5f00\u5fc3\u4e86\u3002<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"486\" height=\"110\" src=\"\/wp-content\/uploads\/2020\/02\/\u56fe\u7247-73.png\" alt=\"\" class=\"wp-image-3018\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2020\/02\/\u56fe\u7247-73.png 486w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2020\/02\/\u56fe\u7247-73-300x68.png 300w\" sizes=\"(max-width: 486px) 100vw, 486px\" \/><\/figure><\/div>\n\n\n<p>\u7b80\u5355\u8bf4\u8fd9\u9053\u9898\u4f9d\u7136\u662f\u5185\u5b58\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\uff0c\u56e0\u4e3a\u8fd9\u6b21\u53d6\u6700\u5c0f\uff0c\u6240\u4ee5\u6211\u4eec\u5c06\u6700\u540e\u4e00\u4f4d\u7f6e\u4e3a\u6700\u5927\u3002\u540c\u6837\uff0c\u56e0\u4e3a\u5f53\u524d\u5c42\u662f\u5bf9\u4e0a\u4e00\u5c42\u8fdb\u884c\u7684\u64cd\u4f5c\uff0c\u56e0\u6b64\u53ef\u4ee5\u5c06\u4e8c\u7ef4dp\u6570\u7ec4\u964d\u4e3a\u4e00\u7ef4\uff0c\u4f7f\u7528<code>dp[j] = min(dp[j - 1] + grid[i][j], dp[j] + grid[i][j])<\/code>\u6765\u8ba1\u7b97i j\u4f4d\u7f6e\u7684dp\u7ed3\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode-cn.com\/problems\/minimum-path-sum\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10],"tags":[],"views":2121,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/3017"}],"collection":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/comments?post=3017"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/3017\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=3017"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=3017"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=3017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}