{"id":2891,"date":"2018-06-03T10:02:36","date_gmt":"2018-06-03T17:02:36","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2891"},"modified":"2018-06-03T10:07:28","modified_gmt":"2018-06-03T17:07:28","slug":"leetcode-845-longest-mountain-in-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-845-longest-mountain-in-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 845. Longest Mountain in Array"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u627e\u51fa\u6700\u957f\u7684\u5c71\u5f62\u5b50\u6570\u7ec4\u3002<\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/longest-mountain-in-array\/description\/\">https:\/\/leetcode.com\/problems\/longest-mountain-in-array\/description\/<\/a><\/p>\n<p>Let&#8217;s call any (contiguous) subarray B (of A)\u00a0a\u00a0<em>mountain<\/em>\u00a0if the following properties hold:<\/p>\n<ul>\n<li><code>B.length &gt;= 3<\/code><\/li>\n<li>There exists some\u00a0<code>0 &lt; i\u00a0&lt; B.length - 1<\/code>\u00a0such that\u00a0<code>B[0] &lt; B[1] &lt; ... B[i-1] &lt; B[i] &gt; B[i+1] &gt; ... &gt; B[B.length - 1]<\/code><\/li>\n<\/ul>\n<p>(Note that B could be any subarray of A, including the entire array A.)<\/p>\n<p>Given an array\u00a0<code>A<\/code>\u00a0of integers,\u00a0return the length of the longest\u00a0<em>mountain<\/em>.<\/p>\n<p>Return\u00a0<code>0<\/code>\u00a0if there is no mountain.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>[2,1,4,7,3,2,5]\r\n<strong>Output: <\/strong>5\r\n<strong>Explanation: <\/strong>The largest mountain is [1,4,7,3,2] which has length 5.\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false \"><strong>Input: <\/strong>[2,2,2]\r\n<strong>Output: <\/strong>0\r\n<strong>Explanation: <\/strong>There is no mountain.\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>0 &lt;= A.length &lt;= 10000<\/code><\/li>\n<li><code>0 &lt;= A[i] &lt;= 10000<\/code><\/li>\n<\/ol>\n<h1><strong>Solution: DP<\/strong><\/h1>\n<p>Three passes<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 24 ms\r\nclass Solution {\r\npublic:\r\n  int longestMountain(vector&lt;int&gt;&amp; A) {\r\n    vector&lt;int&gt; inc(A.size());\r\n    vector&lt;int&gt; dec(A.size());\r\n    for (int i = 1; i &lt; A.size(); ++i)\r\n      if (A[i] &gt; A[i - 1]) inc[i] = inc[i - 1] + 1;\r\n    for (int i = A.size() - 2; i &gt; 0; --i)\r\n      if (A[i] &gt; A[i + 1]) dec[i] = dec[i + 1] + 1;\r\n    int ans = 0;\r\n    for (int i = 0; i &lt; A.size(); ++i)\r\n      if (inc[i] &amp;&amp; dec[i])\r\n        ans = max(ans, inc[i] + dec[i] + 1);    \r\n    return ans &gt;= 3 ? ans : 0;\r\n  }\r\n};<\/pre>\n<p>One pass<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 24 ms\r\nclass Solution {\r\npublic:\r\n  int longestMountain(vector&lt;int&gt;&amp; A) {\r\n    int inc = 0;\r\n    int dec = 0;\r\n    int ans = 0;\r\n    for (int i = 1; i &lt; A.size(); ++i) {\r\n      if (dec &amp;&amp; A[i] &gt; A[i - 1] || A[i] == A[i - 1]) \r\n        dec = inc = 0;\r\n      inc += A[i] &gt; A[i - 1];\r\n      dec += A[i] &lt; A[i - 1];\r\n      if (inc &amp;&amp; dec)\r\n        ans = max(ans, inc + dec + 1);\r\n    }\r\n    return ans &gt;= 3 ? ans : 0;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem \u9898\u76ee\u5927\u610f\uff1a\u627e\u51fa\u6700\u957f\u7684\u5c71\u5f62\u5b50\u6570\u7ec4\u3002 https:\/\/leetcode.com\/problems\/longest-mountain-in-array\/description\/ Let&#8217;s call any (contiguous) subarray B (of A)\u00a0a\u00a0mountain\u00a0if the following properties hold: B.length &gt;= 3 There exists some\u00a00 &lt; i\u00a0&lt; B.length -&#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,117,177,41],"class_list":["post-2891","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-longest","tag-medium","tag-subarray","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2891","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=2891"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2891\/revisions"}],"predecessor-version":[{"id":2896,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2891\/revisions\/2896"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}