<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>peak Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/peak/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/peak/</link>
	<description></description>
	<lastBuildDate>Wed, 05 Sep 2018 08:31:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>peak Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/peak/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 852. Peak Index in a Mountain Array</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-852-peak-index-in-a-mountain-array/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-852-peak-index-in-a-mountain-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 17 Jun 2018 14:55:07 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[mountain]]></category>
		<category><![CDATA[peak]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2920</guid>

					<description><![CDATA[<p>Problem Let&#8217;s call an array A a mountain if the following properties hold: A.length &#62;= 3 There exists some 0 &#60; i &#60; A.length - 1 such that A[0] &#60; A[1] &#60; ...&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-852-peak-index-in-a-mountain-array/">花花酱 LeetCode 852. Peak Index in a Mountain Array</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>Let&#8217;s call an array <code>A</code> a <em>mountain</em> if the following properties hold:</p>
<ul>
<li><code>A.length &gt;= 3</code></li>
<li>There exists some <code>0 &lt; i &lt; A.length - 1</code> such that <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>
</ul>
<p>Given an array that is definitely a mountain, return any <code>i</code> such that <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>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false "><strong>Input: </strong><span id="example-input-1-1">[0,1,0]</span>
<strong>Output: </strong><span id="example-output-1">1</span>
</pre>
<div>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-2-1">[0,2,1,0]</span>
<strong>Output: </strong><span id="example-output-2">1</span></pre>
</div>
<p><strong>Note:</strong></p>
<ol>
<li><code>3 &lt;= A.length &lt;= 10000</code></li>
<li><span style="font-family: monospace;">0 &lt;= A[i] &lt;= 10^6</span></li>
<li>A is a mountain, as defined above.</li>
</ol>
<h1><strong>Solution1: Liner Scan</strong></h1>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 16 ms
class Solution {
public:
  int peakIndexInMountainArray(vector&lt;int&gt;&amp; A) {
    for (int i = 1; i &lt; A.size(); ++i)
      if (A[i] &lt; A[i - 1]) return i - 1;
  }
};</pre><p></div><h2 class="tabtitle">C++/STL</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 16 ms
class Solution {
public:
  int peakIndexInMountainArray(vector&lt;int&gt;&amp; A) {
    return max_element(begin(A), end(A)) - begin(A);
  }
};</pre><p></div></div></p>
<h1><strong>Solution 2: Binary Search</strong></h1>
<p>Find the smallest l such that A[l] &gt; A[l + 1].</p>
<p>Time complexity: O(logn)</p>
<p>Space complexity: O(1)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 18 ms
class Solution {
public:
  int peakIndexInMountainArray(vector&lt;int&gt;&amp; A) {
    int l = 0;
    int r = A.size();
    while (l &lt; r) {
      int m = l + (r - l) / 2;
      if (A[m] &gt; A[m + 1])
        r = m;
      else
        l = m + 1;
    }
    return l;
  }
};</pre><p></div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua, 3 ms
class Solution {
  public int peakIndexInMountainArray(int[] A) {
    int l = 0;
    int r = A.length;
    while (l &lt; r) {
      int m = l + (r - l) / 2;
      if (A[m] &gt; A[m + 1])
        r = m;
      else
        l = m + 1;
    }
    return l;
  }
}</pre><p></div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag"># Author: Huahua, 40 ms
class Solution:
  def peakIndexInMountainArray(self, A):
    l, r = 0, len(A)
    while l &lt; r:
      m = l + (r - l) // 2
      if A[m] &gt; A[m + 1]:
        r = m
      else:
        l = m + 1
    return l</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-852-peak-index-in-a-mountain-array/">花花酱 LeetCode 852. Peak Index in a Mountain Array</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-852-peak-index-in-a-mountain-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
