{"id":2839,"date":"2018-05-24T21:55:24","date_gmt":"2018-05-25T04:55:24","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2839"},"modified":"2018-05-24T23:12:04","modified_gmt":"2018-05-25T06:12:04","slug":"leetcode-329-longest-increasing-path-in-a-matrix","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-329-longest-increasing-path-in-a-matrix\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 329. Longest Increasing Path in a Matrix"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 329. Longest Increasing Path in a Matrix  - \u5237\u9898\u627e\u5de5\u4f5c EP189\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/yKr4iyQnBpY?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<h1><strong>Problem<\/strong><\/h1>\n<p>Given an integer matrix, find the length of the longest increasing path.<\/p>\n<p>From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed).<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>nums = \r\n[\r\n  [<span style=\"color: red;\">9<\/span>,9,4],\r\n  [<span style=\"color: red;\">6<\/span>,6,8],\r\n  [<span style=\"color: red;\">2<\/span>,<span style=\"color: red;\">1<\/span>,1]\r\n] \r\n<strong>Output:<\/strong> 4 \r\n<strong>Explanation:<\/strong> The longest increasing path is <code>[1, 2, 6, 9]<\/code>.<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> nums = \r\n[\r\n  [<span style=\"color: red;\">3<\/span>,<span style=\"color: red;\">4<\/span>,<span style=\"color: red;\">5<\/span>],\r\n  [3,2,<span style=\"color: red;\">6<\/span>],\r\n  [2,2,1]\r\n] \r\n<strong>Output: <\/strong>4 \r\n<strong>Explanation: <\/strong>The longest increasing path is <code>[3, 4, 5, 6]<\/code>. Moving diagonally is not allowed.<\/pre>\n<h1><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2845\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/05\/329-ep189.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/05\/329-ep189.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/05\/329-ep189-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/05\/329-ep189-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/h1>\n<h1><strong>Solution1:\u00a0DFS + Memorization<\/strong><\/h1>\n<p>Time complexity: O(mn)<\/p>\n<p>Space complexity: O(mn)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 36 ms\r\nclass Solution {\r\npublic:\r\n  int longestIncreasingPath(vector&lt;vector&lt;int&gt;&gt;&amp; matrix) {\r\n    if (matrix.empty()) return 0;\r\n    m_ = matrix.size();\r\n    n_ = matrix[0].size();\r\n    dp_ = vector&lt;vector&lt;int&gt;&gt;(m_, vector&lt;int&gt;(n_, -1)); \r\n    int ans = 0;\r\n    for (int y = 0; y &lt; m_; ++y)\r\n      for (int x = 0; x &lt; n_; ++x)\r\n        ans = max(ans, dfs(matrix, x, y));\r\n    return ans;\r\n  }\r\nprivate:\r\n  int dfs(const vector&lt;vector&lt;int&gt;&gt;&amp; mat, int x, int y) {\r\n    if (dp_[y][x] != -1) return dp_[y][x];\r\n    static int dirs[] = {0, -1, 0, 1, 0};\r\n    dp_[y][x] = 1;\r\n    for (int i = 0; i &lt; 4; ++i) {\r\n      int tx = x + dirs[i];\r\n      int ty = y + dirs[i + 1];\r\n      if (tx &lt; 0 || ty &lt; 0 || tx &gt;= n_ || ty &gt;= m_ || mat[ty][tx] &lt;= mat[y][x]) \r\n        continue;\r\n      dp_[y][x] = max(dp_[y][x], 1 + dfs(mat, tx, ty));\r\n    }\r\n    return dp_[y][x];\r\n  }\r\n  \r\n  int m_;\r\n  int n_;\r\n  \/\/ dp[i][j]: max length start from (j, i)\r\n  vector&lt;vector&lt;int&gt;&gt; dp_;  \r\n};<\/pre>\n<h1><strong>Solution2:\u00a0DP<\/strong><\/h1>\n<p>DP<\/p>\n<p>Time complexity: O(mn*log(mn))<\/p>\n<p>Space complexity: O(mn)<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 63 ms\r\nclass Solution {\r\npublic:\r\n  int longestIncreasingPath(vector&lt;vector&lt;int&gt;&gt;&amp; matrix) {\r\n    if (matrix.empty()) return 0;\r\n    int m = matrix.size();\r\n    int n = matrix[0].size();\r\n    vector&lt;vector&lt;int&gt;&gt; dp(m, vector&lt;int&gt;(n, 1)); \r\n    int ans = 0;\r\n    \r\n    vector&lt;pair&lt;int, pair&lt;int, int&gt;&gt;&gt; cells;\r\n    for (int y = 0; y &lt; m; ++y)\r\n      for (int x = 0; x &lt; n; ++x)\r\n        cells.push_back({matrix[y][x], {x, y}});\r\n    sort(cells.rbegin(), cells.rend());\r\n    \r\n    vector&lt;int&gt; dirs {0, 1, 0, -1, 0};    \r\n    for (const auto&amp; cell : cells) {\r\n      int x = cell.second.first;\r\n      int y = cell.second.second;\r\n      for (int i = 0; i &lt; 4; ++i) {\r\n        int tx = x + dirs[i];\r\n        int ty = y + dirs[i + 1];\r\n        if (tx &lt; 0 || tx &gt;= n || ty &lt; 0 || ty &gt;= m) continue;\r\n        if (matrix[ty][tx] &lt;= matrix[y][x]) continue;\r\n        dp[y][x] = max(dp[y][x], 1 + dp[ty][tx]);            \r\n      }\r\n      ans = max(ans, dp[y][x]);\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given an integer matrix, find the length of the longest increasing path. From each cell, you can either move to four directions: left, right,&#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":[33,18,116],"class_list":["post-2839","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dfs","tag-dp","tag-path","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2839","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=2839"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2839\/revisions"}],"predecessor-version":[{"id":2846,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2839\/revisions\/2846"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2839"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2839"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2839"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}