{"id":2555,"date":"2018-05-06T20:45:20","date_gmt":"2018-05-06T12:45:20","guid":{"rendered":"https:\/\/boweihe.me\/?p=2555"},"modified":"2018-05-06T20:45:20","modified_gmt":"2018-05-06T12:45:20","slug":"leetcode-lis%e7%b3%bb%e5%88%97","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2555","title":{"rendered":"LeetCode LIS\u7cfb\u5217 (Longest Increasing Path in a Matrix)"},"content":{"rendered":"<p>\u7c7b\u4f3c\u7684\u4e1c\u897f\u8fd8\u6709\uff1a<br \/>\n<strong>674.\u00a0Longest Continuous Increasing Subsequence<\/strong><br \/>\nhttps:\/\/leetcode.com\/problems\/longest-continuous-increasing-subsequence\/description\/<br \/>\n<strong>300.\u00a0Longest Increasing Subsequence<\/strong><br \/>\nhttps:\/\/leetcode.com\/problems\/longest-increasing-subsequence\/description\/<br \/>\n<strong>longest path increasing by 1<\/strong><br \/>\nhttps:\/\/www.geeksforgeeks.org\/find-the-longest-path-in-a-matrix-with-given-constraints\/<br \/>\n&nbsp;<br \/>\n\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u8fd9\u4e2a\u9898\u7684\uff1a<\/p>\n<div class=\"col-lg-8 col-md-7 col-sm-6 col-sm-pull-6 col-md-pull-5 col-lg-pull-4\">\n<h3>329.\u00a0Longest Increasing Path in a Matrix<\/h3>\n<\/div>\n<p><a href=\"https:\/\/leetcode.com\/problems\/longest-increasing-path-in-a-matrix\/description\/\">https:\/\/leetcode.com\/problems\/longest-increasing-path-in-a-matrix\/description\/\u00a0<\/a><br \/>\n\u4e3b\u8981\u601d\u8def\u5c31\u662f\u778eJB\u641c\u7d22\uff0c\u4e0d\u8fc7\u67d0\u4e00\u4e2a\u4f4d\u7f6e\u7684\u7ed3\u679c\u53ef\u4ee5\u8bb0\u5f55\u4e0b\u6765\u7ed9\u540e\u7eed\u7684\u91cd\u590d\u641c\u7d22\u4f7f\u7528\u3002<\/p>\n<pre class=\"theme:github lang:c++ decode:true \">class Solution {\npublic:\n\tint subLIP(vector&lt;vector&lt;int&gt;&gt;&amp; matrix, vector&lt;vector&lt;int&gt;&gt;&amp; sub_results, int row, int col)\n\t{\n\t\tif (row &lt; 0 || row &gt;= matrix.size())\n\t\t\treturn 0;\n\t\tif (col &lt; 0 || col &gt;= matrix[0].size())\n\t\t\treturn 0;\n\t\tif (sub_results[row][col] != -1)\n\t\t\treturn sub_results[row][col];\n\t\t\/\/ go left\n\t\tint maxResult = -1;\n\t\tif (col &gt; 0 &amp;&amp; matrix[row][col - 1] &gt; matrix[row][col])\n\t\t\tmaxResult = max(maxResult, 1 + subLIP(matrix, sub_results, row, col - 1));\n\t\t\/\/ go right\n\t\tif (col &lt; matrix[0].size() - 1 &amp;&amp; matrix[row][col + 1] &gt; matrix[row][col])\n\t\t\tmaxResult = max(maxResult, 1 + subLIP(matrix, sub_results, row, col + 1));\n\t\t\/\/ go up\n\t\tif (row &gt; 0 &amp;&amp; matrix[row - 1][col] &gt; matrix[row][col])\n\t\t\tmaxResult = max(maxResult, 1 + subLIP(matrix, sub_results, row - 1, col));\n\t\t\/\/ go down\n\t\tif (row &lt; matrix.size() - 1 &amp;&amp; matrix[row + 1][col] &gt; matrix[row][col])\n\t\t\tmaxResult = max(maxResult, 1 + subLIP(matrix, sub_results, row + 1, col));\n\t\tsub_results[row][col] = max(1, maxResult);\n\t\treturn sub_results[row][col];\n\t}\n\tint longestIncreasingPath(vector&lt;vector&lt;int&gt;&gt;&amp; matrix) {\n\t\tvector&lt;vector&lt;int&gt;&gt; sub_results;\n\t\tsub_results.resize(matrix.size());\n\t\tfor (int i = 0; i &lt; matrix.size(); ++i)\n\t\t{\n\t\t\tsub_results[i].resize(matrix[0].size(), -1);   \/\/ -1 means unknown\n\t\t}\n\t\tint result = 0;\n\t\tfor (int r = 0; r &lt; matrix.size(); ++r)\n\t\t{\n\t\t\tfor (int c = 0; c &lt; matrix[r].size(); ++c)\n\t\t\t{\n\t\t\t\tresult = max(result, subLIP(matrix, sub_results, r, c));\n\t\t\t}\n\t\t}\n\t\treturn result;\n\t}\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7c7b\u4f3c\u7684\u4e1c\u897f\u8fd8\u6709\uff1a 674.\u00a0Longest Continuous Increasing Subsequence https:\/\/leetcode.com\/problems\/longest-continuous-increasing-subsequence\/description\/ 300.\u00a0Longest Increasing Subsequence https:\/\/leetcode.com\/problems\/longest-increasing-subsequence\/description\/ longest path increasing by 1 https:\/\/www.geeksforgeeks.org\/find-the-longest-path-in-a-matrix-with-given-constraints\/ &nbsp; \u4e0b\u9762\u7684\u4ee3\u7801\u662f\u8fd9\u4e2a\u9898\u7684\uff1a 329.\u00a0Longest Increasing Path in a Matrix https:\/\/leetcode.com\/problems\/longest-increasing-path-in-a-matrix\/description\/\u00a0 \u4e3b\u8981\u601d\u8def\u5c31\u662f\u778eJB\u641c\u7d22\uff0c\u4e0d\u8fc7\u67d0\u4e00\u4e2a\u4f4d\u7f6e\u7684\u7ed3\u679c\u53ef\u4ee5\u8bb0\u5f55\u4e0b\u6765\u7ed9\u540e\u7eed\u7684\u91cd\u590d\u641c\u7d22\u4f7f\u7528\u3002 class Solution { public: int subLIP(vector&lt;vector&lt;int&gt;&gt;&amp; matrix, vector&lt;vector&lt;int&gt;&gt;&amp; sub_results, int row, int col) { if (row &lt; 0 || row &gt;= matrix.size()) return 0; if (col &lt; 0 || [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[66],"class_list":["post-2555","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2555","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2555"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2555\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}