{"id":982,"date":"2017-11-27T22:33:06","date_gmt":"2017-11-28T06:33:06","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=982"},"modified":"2018-04-19T08:40:01","modified_gmt":"2018-04-19T15:40:01","slug":"leetcode-64-minimum-path-sum","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-64-minimum-path-sum\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 64. Minimum Path Sum"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 64. Minimum Path Sum - \u5237\u9898\u627e\u5de5\u4f5c EP115\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/Num2bcAu0Sk?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>Given a\u00a0<i>m<\/i>\u00a0x\u00a0<i>n<\/i>\u00a0grid filled with non-negative numbers, find a path from top left to bottom right which\u00a0<i>minimizes<\/i>\u00a0the sum of all numbers along its path.<\/p>\n<p><b>Note:<\/b>\u00a0You can only move either down or right at any point in time.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre>[[1,3,1],\r\n [1,5,1],\r\n [4,2,1]]\r\n<\/pre>\n<p>Given the above grid map, return\u00a0<code>7<\/code>. Because the path 1\u21923\u21921\u21921\u21921 minimizes the sum.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p>DP<\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/64-ep115.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-989\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/64-ep115.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/64-ep115.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/64-ep115-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/64-ep115-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><strong>Solution 1:<\/strong><\/p>\n<p>C++ \/ Recursion with memoization<\/p>\n<p>Time complexity: O(mn)<\/p>\n<p>Space complexity: O(mn)<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Runtime: 9 ms\r\nclass Solution {\r\npublic:\r\n    int minPathSum(vector&lt;vector&lt;int&gt;&gt;&amp; grid) {\r\n        int m = grid.size();\r\n        if (m == 0) return 0;\r\n        int n = grid[0].size();\r\n        \r\n        s_ = vector&lt;vector&lt;int&gt;&gt;(m, vector&lt;int&gt;(n, 0));\r\n        \r\n        return minPathSum(grid, n - 1, m - 1, n, m);\r\n    }    \r\nprivate:\r\n    long minPathSum(const vector&lt;vector&lt;int&gt;&gt;&amp; grid, \r\n                   int x, int y, int n, int m) {        \r\n        if (x == 0 &amp;&amp; y == 0) return grid[y][x];\r\n        if (x &lt; 0 || y &lt; 0) return INT_MAX;\r\n        if (s_[y][x] &gt; 0) return s_[y][x];\r\n        \r\n        int ans = grid[y][x] + min(minPathSum(grid, x - 1, y, n, m),\r\n                                   minPathSum(grid, x, y - 1, n, m));\r\n        return s_[y][x] = ans;\r\n    }\r\n    \r\n    vector&lt;vector&lt;int&gt;&gt; s_;\r\n};<\/pre>\n<p><strong>Solution 2:<\/strong><\/p>\n<p>C++ \/ DP<\/p>\n<p>Time complexity: O(mn)<\/p>\n<p>Space complexity: O(1)<\/p>\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Runtime: 9 ms\r\nclass Solution {\r\npublic:\r\n    int minPathSum(vector&lt;vector&lt;int&gt;&gt;&amp; grid) {\r\n        int m = grid.size();\r\n        if (m == 0) return 0;\r\n        int n = grid[0].size();\r\n        \r\n        for (int i = 0; i &lt; m; ++i)\r\n            for (int j = 0; j &lt; n; ++j) {\r\n                if (i == 0 &amp;&amp; j == 0) continue;\r\n                if (i == 0) \r\n                    grid[i][j] += grid[i][j - 1];\r\n                else if (j == 0)\r\n                    grid[i][j] += grid[i - 1][j];\r\n                else\r\n                    grid[i][j] += min(grid[i][j - 1], grid[i - 1][j]);\r\n            }\r\n        \r\n        return grid[m - 1][n - 1];\r\n    }    \r\n};<\/pre>\n<p><strong>Related Problems:<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-113-path-sum-ii\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 113. Path Sum II<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Given a\u00a0m\u00a0x\u00a0n\u00a0grid filled with non-negative numbers, find a path from top left to bottom right which\u00a0minimizes\u00a0the sum of all numbers along its path. Note:\u00a0You&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46,164],"tags":[18,167,17],"class_list":["post-982","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","category-medium","tag-dp","tag-memoization","tag-recursion","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/982","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=982"}],"version-history":[{"count":8,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/982\/revisions"}],"predecessor-version":[{"id":2692,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/982\/revisions\/2692"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}