|
| 1 | +<h2>852. Peak Index in a Mountain Array</h2><h3>Easy</h3><hr><div><p>Let's call an array <code>arr</code> a <strong>mountain</strong> if the following properties hold:</p> |
| 2 | + |
| 3 | +<ul> |
| 4 | + <li><code>arr.length >= 3</code></li> |
| 5 | + <li>There exists some <code>i</code> with <code>0 < i < arr.length - 1</code> such that: |
| 6 | + <ul> |
| 7 | + <li><code>arr[0] < arr[1] < ... arr[i-1] < arr[i] </code></li> |
| 8 | + <li><code>arr[i] > arr[i+1] > ... > arr[arr.length - 1]</code></li> |
| 9 | + </ul> |
| 10 | + </li> |
| 11 | +</ul> |
| 12 | + |
| 13 | +<p>Given an integer array <code>arr</code> that is <strong>guaranteed</strong> to be a mountain, return any <code>i</code> such that <code>arr[0] < arr[1] < ... arr[i - 1] < arr[i] > arr[i + 1] > ... > arr[arr.length - 1]</code>.</p> |
| 14 | + |
| 15 | +<p> </p> |
| 16 | +<p><strong>Example 1:</strong></p> |
| 17 | +<pre><strong>Input:</strong> arr = [0,1,0] |
| 18 | +<strong>Output:</strong> 1 |
| 19 | +</pre><p><strong>Example 2:</strong></p> |
| 20 | +<pre><strong>Input:</strong> arr = [0,2,1,0] |
| 21 | +<strong>Output:</strong> 1 |
| 22 | +</pre><p><strong>Example 3:</strong></p> |
| 23 | +<pre><strong>Input:</strong> arr = [0,10,5,2] |
| 24 | +<strong>Output:</strong> 1 |
| 25 | +</pre><p><strong>Example 4:</strong></p> |
| 26 | +<pre><strong>Input:</strong> arr = [3,4,5,1] |
| 27 | +<strong>Output:</strong> 2 |
| 28 | +</pre><p><strong>Example 5:</strong></p> |
| 29 | +<pre><strong>Input:</strong> arr = [24,69,100,99,79,78,67,36,26,19] |
| 30 | +<strong>Output:</strong> 2 |
| 31 | +</pre> |
| 32 | +<p> </p> |
| 33 | +<p><strong>Constraints:</strong></p> |
| 34 | + |
| 35 | +<ul> |
| 36 | + <li><code>3 <= arr.length <= 10<sup>4</sup></code></li> |
| 37 | + <li><code>0 <= arr[i] <= 10<sup>6</sup></code></li> |
| 38 | + <li><code>arr</code> is <strong>guaranteed</strong> to be a mountain array.</li> |
| 39 | +</ul> |
| 40 | + |
| 41 | +<p> </p> |
| 42 | +<strong>Follow up:</strong> Finding the <code>O(n)</code> is straightforward, could you find an <code>O(log(n))</code> solution?</div> |
0 commit comments