{"id":4331,"date":"2018-11-18T09:32:59","date_gmt":"2018-11-18T17:32:59","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4331"},"modified":"2018-11-18T09:34:53","modified_gmt":"2018-11-18T17:34:53","slug":"leetcode-941-valid-mountain-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-941-valid-mountain-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 941. Valid Mountain Array"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given an array\u00a0<code>A<\/code>\u00a0of integers, return\u00a0<code>true<\/code>\u00a0if and only if it is a\u00a0<em>valid mountain array<\/em>.<\/p>\n<p>Recall that A is a mountain array if and only if:<\/p>\n<ul>\n<li><code>A.length &gt;= 3<\/code><\/li>\n<li>There exists some\u00a0<code>i<\/code>\u00a0with\u00a0<code>0 &lt; i\u00a0&lt; A.length - 1<\/code>\u00a0such that:\n<ul>\n<li><code>A[0] &lt; A[1] &lt; ... A[i-1] &lt; A[i]<\/code><\/li>\n<li><code>A[i] &gt; A[i+1] &gt; ... &gt; A[B.length - 1]<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-1-1\">[2,1]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">false<\/span>\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-2-1\">[3,5,5]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">false<\/span>\r\n<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-3-1\">[0,3,2,1]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-3\">true<\/span><\/pre>\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\u00a0<\/code><\/li>\n<\/ol>\n<h1>Solution<\/h1>\n<p>Use has_up and has_down to track whether we have A[i] &gt; A[i &#8211; 1] and A[i] &lt; A[i &#8211; 1] receptively.<\/p>\n<p>return false if any of the following happened:<\/p>\n<ol>\n<li>size(A) &lt; 3<\/li>\n<li>has_down happened before has_up<\/li>\n<li>not has_down or not has_up<\/li>\n<li>A[i &#8211; 1] &lt; A[i] after has_down<\/li>\n<li>A[i &#8211; 1] &gt; A[i] before has_up<\/li>\n<\/ol>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua, 24 ms\r\nclass Solution {\r\npublic:\r\n  bool validMountainArray(vector&lt;int&gt;&amp; A) {\r\n    if (A.size() &lt; 3) return false;\r\n    bool has_up = false;\r\n    bool has_down = false;\r\n    for (int i = 1; i &lt; A.size(); ++i) {\r\n      if (A[i] &gt; A[i - 1]) {\r\n        if (has_down) return false;\r\n        has_up = true;\r\n      }\r\n      else if (A[i] &lt; A[i - 1]){\r\n        if (!has_up) return false;\r\n        has_down = true;\r\n      } else {\r\n        return false;\r\n      }\r\n    }\r\n    return has_up &amp;&amp; has_down;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given an array\u00a0A\u00a0of integers, return\u00a0true\u00a0if and only if it is a\u00a0valid mountain array. Recall that A is a mountain array if and only if:&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,222,310],"class_list":["post-4331","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","tag-mountain","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4331","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=4331"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4331\/revisions"}],"predecessor-version":[{"id":4333,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4331\/revisions\/4333"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}