{"id":2920,"date":"2018-06-17T07:55:07","date_gmt":"2018-06-17T14:55:07","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2920"},"modified":"2018-09-05T01:31:41","modified_gmt":"2018-09-05T08:31:41","slug":"leetcode-852-peak-index-in-a-mountain-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-852-peak-index-in-a-mountain-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 852. Peak Index in a Mountain Array"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Let&#8217;s call an array\u00a0<code>A<\/code>\u00a0a\u00a0<em>mountain<\/em>\u00a0if the following properties hold:<\/p>\n<ul>\n<li><code>A.length &gt;= 3<\/code><\/li>\n<li>There exists some\u00a0<code>0 &lt; i\u00a0&lt; A.length - 1<\/code>\u00a0such that\u00a0<code>A[0] &lt; A[1] &lt; ... A[i-1] &lt; A[i] &gt; A[i+1] &gt; ... &gt; A[A.length - 1]<\/code><\/li>\n<\/ul>\n<p>Given an array that is definitely a mountain, return any\u00a0<code>i<\/code>\u00a0such that\u00a0<code>A[0] &lt; A[1] &lt; ... A[i-1] &lt; A[i] &gt; A[i+1] &gt; ... &gt; A[A.length - 1]<\/code>.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false \"><strong>Input: <\/strong><span id=\"example-input-1-1\">[0,1,0]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">1<\/span>\r\n<\/pre>\n<div>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-2-1\">[0,2,1,0]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">1<\/span><\/pre>\n<\/div>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>3 &lt;= A.length &lt;= 10000<\/code><\/li>\n<li><span style=\"font-family: monospace;\">0 &lt;= A[i] &lt;= 10^6<\/span><\/li>\n<li>A\u00a0is a mountain, as defined above.<\/li>\n<\/ol>\n<h1><strong>Solution1: Liner Scan<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/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\r\n\/\/ Running time: 16 ms\r\nclass Solution {\r\npublic:\r\n  int peakIndexInMountainArray(vector&lt;int&gt;&amp; A) {\r\n    for (int i = 1; i &lt; A.size(); ++i)\r\n      if (A[i] &lt; A[i - 1]) return i - 1;\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">C++\/STL<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 16 ms\r\nclass Solution {\r\npublic:\r\n  int peakIndexInMountainArray(vector&lt;int&gt;&amp; A) {\r\n    return max_element(begin(A), end(A)) - begin(A);\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n<h1><strong>Solution 2: Binary Search<\/strong><\/h1>\n<p>Find the smallest l such that A[l] &gt; A[l + 1].<\/p>\n<p>Time complexity: O(logn)<\/p>\n<p>Space complexity: O(1)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 18 ms\r\nclass Solution {\r\npublic:\r\n  int peakIndexInMountainArray(vector&lt;int&gt;&amp; A) {\r\n    int l = 0;\r\n    int r = A.size();\r\n    while (l &lt; r) {\r\n      int m = l + (r - l) \/ 2;\r\n      if (A[m] &gt; A[m + 1])\r\n        r = m;\r\n      else\r\n        l = m + 1;\r\n    }\r\n    return l;\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:java decode:true\">\/\/ Author: Huahua, 3 ms\r\nclass Solution {\r\n  public int peakIndexInMountainArray(int[] A) {\r\n    int l = 0;\r\n    int r = A.length;\r\n    while (l &lt; r) {\r\n      int m = l + (r - l) \/ 2;\r\n      if (A[m] &gt; A[m + 1])\r\n        r = m;\r\n      else\r\n        l = m + 1;\r\n    }\r\n    return l;\r\n  }\r\n}<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true \"># Author: Huahua, 40 ms\r\nclass Solution:\r\n  def peakIndexInMountainArray(self, A):\r\n    l, r = 0, len(A)\r\n    while l &lt; r:\r\n      m = l + (r - l) \/\/ 2\r\n      if A[m] &gt; A[m + 1]:\r\n        r = m\r\n      else:\r\n        l = m + 1\r\n    return l<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Let&#8217;s call an array\u00a0A\u00a0a\u00a0mountain\u00a0if the following properties hold: A.length &gt;= 3 There exists some\u00a00 &lt; i\u00a0&lt; A.length &#8211; 1\u00a0such that\u00a0A[0] &lt; A[1] &lt; &#8230;&#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,149],"tags":[52,222,310,311],"class_list":["post-2920","post","type-post","status-publish","format-standard","hentry","category-array","category-binary-search","tag-binary-search","tag-easy","tag-mountain","tag-peak","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2920","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=2920"}],"version-history":[{"count":7,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2920\/revisions"}],"predecessor-version":[{"id":3874,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2920\/revisions\/3874"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2920"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}