2023-12-09 18:42:21 +08:00
|
|
|
<p>An array <code>arr</code> is a <strong>mountain</strong> if the following properties hold:</p>
|
2022-03-27 18:35:17 +08:00
|
|
|
|
|
|
|
<ul>
|
|
|
|
<li><code>arr.length >= 3</code></li>
|
2023-12-09 18:42:21 +08:00
|
|
|
<li>There exists some <code>i</code> with <code>0 < i < arr.length - 1</code> such that:
|
2022-03-27 18:35:17 +08:00
|
|
|
<ul>
|
2023-12-09 18:42:21 +08:00
|
|
|
<li><code>arr[0] < arr[1] < ... < arr[i - 1] < arr[i] </code></li>
|
|
|
|
<li><code>arr[i] > arr[i + 1] > ... > arr[arr.length - 1]</code></li>
|
2022-03-27 18:35:17 +08:00
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
|
2023-12-09 18:42:21 +08:00
|
|
|
<p>Given a mountain array <code>arr</code>, return the index <code>i</code> such that <code>arr[0] < arr[1] < ... < arr[i - 1] < arr[i] > arr[i + 1] > ... > arr[arr.length - 1]</code>.</p>
|
|
|
|
|
|
|
|
<p>You must solve it in <code>O(log(arr.length))</code> time complexity.</p>
|
2022-03-27 18:35:17 +08:00
|
|
|
|
|
|
|
<p> </p>
|
2023-12-09 18:42:21 +08:00
|
|
|
<p><strong class="example">Example 1:</strong></p>
|
2022-03-27 18:35:17 +08:00
|
|
|
|
|
|
|
<pre>
|
|
|
|
<strong>Input:</strong> arr = [0,1,0]
|
|
|
|
<strong>Output:</strong> 1
|
|
|
|
</pre>
|
|
|
|
|
2023-12-09 18:42:21 +08:00
|
|
|
<p><strong class="example">Example 2:</strong></p>
|
2022-03-27 18:35:17 +08:00
|
|
|
|
|
|
|
<pre>
|
|
|
|
<strong>Input:</strong> arr = [0,2,1,0]
|
|
|
|
<strong>Output:</strong> 1
|
|
|
|
</pre>
|
|
|
|
|
2023-12-09 18:42:21 +08:00
|
|
|
<p><strong class="example">Example 3:</strong></p>
|
2022-03-27 18:35:17 +08:00
|
|
|
|
|
|
|
<pre>
|
|
|
|
<strong>Input:</strong> arr = [0,10,5,2]
|
|
|
|
<strong>Output:</strong> 1
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
<p> </p>
|
|
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
|
|
|
|
<ul>
|
2023-12-09 18:42:21 +08:00
|
|
|
<li><code>3 <= arr.length <= 10<sup>5</sup></code></li>
|
2022-03-27 18:35:17 +08:00
|
|
|
<li><code>0 <= arr[i] <= 10<sup>6</sup></code></li>
|
|
|
|
<li><code>arr</code> is <strong>guaranteed</strong> to be a mountain array.</li>
|
|
|
|
</ul>
|