{"id":187,"date":"2017-09-09T17:25:07","date_gmt":"2017-09-10T00:25:07","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=187"},"modified":"2018-08-01T21:31:00","modified_gmt":"2018-08-02T04:31:00","slug":"leetcode-62-unique-paths","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-62-unique-paths\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 62. Unique Paths"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 62. Unique Paths - \u5237\u9898\u627e\u5de5\u4f5c EP45\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/fmpP5Ll0Azc?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>A robot is located at the top-left corner of a\u00a0<i>m<\/i>\u00a0x\u00a0<i>n<\/i>\u00a0grid (marked &#8216;Start&#8217; in the diagram below).<\/p>\n<p>The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked &#8216;Finish&#8217; in the diagram below).<\/p>\n<p>How many possible unique paths are there?<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/leetcode.com\/static\/images\/problemset\/robot_maze.png\" \/><\/p>\n<p>Above is a 3 x 7 grid. How many possible unique paths are there?<\/p>\n<p><b>Note:<\/b>\u00a0<i>m<\/i>\u00a0and\u00a0<i>n<\/i>\u00a0will be at most 100.<\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p>Dynamic Programming<\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/62-ep45.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-196\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/62-ep45.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/62-ep45.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/62-ep45-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/62-ep45-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/62-ep45-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><strong>Solution1:<\/strong><\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\nclass Solution {    \r\npublic:\r\n    int uniquePaths(int m, int n) {\r\n        if (m &lt; 0 || n &lt; 0) return 0;\r\n        if (m == 1 &amp;&amp; n == 1) return 1;\r\n        if (f_[m][n] &gt; 0) return f_[m][n];        \r\n        int left_paths = uniquePaths(m - 1, n);\r\n        int top_paths = uniquePaths(m, n - 1);\r\n        f_[m][n] = left_paths + top_paths;\r\n        return f_[m][n];\r\n    }\r\nprivate:\r\n    unordered_map&lt;int, unordered_map&lt;int, int&gt;&gt; f_;\r\n};<\/pre>\n<p>Java<\/p>\n<pre class=\"lang:java decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 0 ms\r\nclass Solution {\r\n  private int[][] dp;\r\n  private int m;\r\n  private int n;\r\n  \r\n  public int uniquePaths(int m, int n) {\r\n    this.dp = new int[m][n];    \r\n    this.m = m;\r\n    this.n = n;\r\n    return dfs(0, 0);\r\n  }\r\n  \r\n  private int dfs(int x, int y) {\r\n    if (x &gt; m - 1 || y &gt; n - 1)\r\n      return 0;\r\n    if (x == m - 1 &amp;&amp; y == n - 1)\r\n      return 1;\r\n    if (dp[x][y] == 0)     \r\n      dp[x][y] = dfs(x + 1, y) + dfs(x , y + 1);\r\n    return dp[x][y];\r\n  } \r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Solution2:<\/strong><\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\nclass Solution {    \r\npublic:\r\n    int uniquePaths(int m, int n) {\r\n        auto f = vector&lt;vector&lt;int&gt;&gt;(n + 1, vector&lt;int&gt;(m + 1, 0));\r\n        f[1][1] = 1;\r\n        \r\n        for (int y = 1; y &lt;= n; ++y)\r\n            for(int x = 1; x &lt;= m; ++x) {\r\n                if (x == 1 &amp;&amp; y == 1) {\r\n                    continue;\r\n                } else {\r\n                    f[y][x] = f[y-1][x] + f[y][x-1];\r\n                }\r\n            }\r\n        \r\n        return f[n][m];\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: A robot is located at the top-left corner of a\u00a0m\u00a0x\u00a0n\u00a0grid (marked &#8216;Start&#8217; in the diagram below). The robot can only move either down or&#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],"tags":[18,177,275,240],"class_list":["post-187","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-medium","tag-paths","tag-unique","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/187","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=187"}],"version-history":[{"count":7,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/187\/revisions"}],"predecessor-version":[{"id":3404,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/187\/revisions\/3404"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}