852:山脉数组的峰顶索引

This commit is contained in:
huangge1199 2021-06-15 12:28:55 +08:00
parent 8de15a737f
commit 3a2a84e6f3
3 changed files with 170 additions and 1 deletions

View File

@ -0,0 +1,105 @@
//符合下列属性的数组 arr 称为 山脉数组
//
// arr.length >= 3
// 存在 i0 < i < arr.length - 1使得
//
// arr[0] < arr[1] < ... arr[i-1] < arr[i]
// arr[i] > arr[i+1] > ... > arr[arr.length - 1]
//
//
//
//
// 给你由整数组成的山脉数组 arr 返回任何满足 arr[0] < arr[1] < ... arr[i - 1] < arr[i] > arr[i +
//1] > ... > arr[arr.length - 1] 的下标 i
//
//
//
// 示例 1
//
//
//输入arr = [0,1,0]
//输出1
//
//
// 示例 2
//
//
//输入arr = [0,2,1,0]
//输出1
//
//
// 示例 3
//
//
//输入arr = [0,10,5,2]
//输出1
//
//
// 示例 4
//
//
//输入arr = [3,4,5,1]
//输出2
//
//
// 示例 5
//
//
//输入arr = [24,69,100,99,79,78,67,36,26,19]
//输出2
//
//
//
//
// 提示
//
//
// 3 <= arr.length <= 104
// 0 <= arr[i] <= 106
// 题目数据保证 arr 是一个山脉数组
//
//
//
//
// 进阶很容易想到时间复杂度 O(n) 的解决方案你可以设计一个 O(log(n)) 的解决方案吗
// Related Topics 二分查找
// 👍 179 👎 0
package leetcode.editor.cn;
//852:山脉数组的峰顶索引
public class PeakIndexInAMountainArray {
public static void main(String[] args) {
//测试代码
Solution solution = new PeakIndexInAMountainArray().new Solution();
System.out.println(solution.peakIndexInMountainArray(new int[]{0, 1, 0}));
System.out.println(solution.peakIndexInMountainArray(new int[]{0, 2, 1, 0}));
System.out.println(solution.peakIndexInMountainArray(new int[]{0, 10, 5, 2}));
System.out.println(solution.peakIndexInMountainArray(new int[]{3, 4, 5, 1}));
System.out.println(solution.peakIndexInMountainArray(new int[]{24, 69, 100, 99, 79, 78, 67, 36, 26, 19}));
System.out.println(solution.peakIndexInMountainArray(new int[]{3,5,3,2,0}));
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int peakIndexInMountainArray(int[] arr) {
int start = 0;
int end = arr.length - 1;
while (start < end) {
int mid = start + (end - start) / 2;
if (arr[mid - 1] < arr[mid] && arr[mid] > arr[mid + 1]) {
return mid;
}
if (arr[mid - 1] > arr[mid]) {
end = mid;
} else {
start = mid;
}
}
return start;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,64 @@
符合下列属性的数组 <code>arr</code> 称为 <strong>山脉数组</strong>
<ul>
<li><code>arr.length >= 3</code></li>
<li>存在 <code>i</code><code>0 < i < arr.length - 1</code>)使得:
<ul>
<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>
</ul>
</li>
</ul>
<p>给你由整数组成的山脉数组 <code>arr</code> ,返回任何满足 <code>arr[0] < arr[1] < ... arr[i - 1] < arr[i] > arr[i + 1] > ... > arr[arr.length - 1]</code> 的下标 <code>i</code></p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>arr = [0,1,0]
<strong>输出:</strong>1
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>arr = [0,2,1,0]
<strong>输出:</strong>1
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>arr = [0,10,5,2]
<strong>输出:</strong>1
</pre>
<p><strong>示例 4</strong></p>
<pre>
<strong>输入:</strong>arr = [3,4,5,1]
<strong>输出:</strong>2
</pre>
<p><strong>示例 5</strong></p>
<pre>
<strong>输入:</strong>arr = [24,69,100,99,79,78,67,36,26,19]
<strong>输出:</strong>2
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>3 <= arr.length <= 10<sup>4</sup></code></li>
<li><code>0 <= arr[i] <= 10<sup>6</sup></code></li>
<li>题目数据保证 <code>arr</code> 是一个山脉数组</li>
</ul>
<p> </p>
<p><strong>进阶:</strong>很容易想到时间复杂度 <code>O(n)</code> 的解决方案,你可以设计一个 <code>O(log(n))</code> 的解决方案吗?</p>
<div><div>Related Topics</div><div><li>二分查找</li></div></div>\n<div><li>👍 179</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long