当前位置:网站首页>LeetCode_二分搜索_中等_162. 寻找峰值
LeetCode_二分搜索_中等_162. 寻找峰值
2022-06-12 11:54:00 【一瓢江湖我沉浮】
1.题目
峰值元素是指其值严格大于左右相邻值的元素。
给你一个整数数组 nums,找到峰值元素并返回其索引。数组可能包含多个峰值,在这种情况下,返回任何一个峰值所在位置即可。
你可以假设 nums[-1] = nums[n] = -∞ 。
你必须实现时间复杂度为 O(log n) 的算法来解决此问题。
示例 1:
输入:nums = [1,2,3,1]
输出:2
解释:3 是峰值元素,你的函数应该返回其索引 2。
示例 2:
输入:nums = [1,2,1,3,5,6,4]
输出:1 或 5
解释:你的函数可以返回索引 1,其峰值元素为 2;或者返回索引 5, 其峰值元素为 6。
提示:
1 <= nums.length <= 1000
-231 <= nums[i] <= 231 - 1
对于所有有效的 i 都有 nums[i] != nums[i + 1]
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/find-peak-element
2.思路
(1)找出最大值(但不满足时间复杂度要求)
由于题目保证了对于所有有效的 i 都有 nums[i] != nums[i + 1],那么数组 nums 中最大值两侧的元素一定严格小于最大值本身。因此,最大值所在的位置就是一个可行的峰值位置。所以只需对数组 nums 进行一次遍历,找到最大值对应对应的下标即可。不过该方法的时间复杂度为 O(n),并不满足题目的要求。
(2)迭代爬坡
思路参考本题官方题解。
(3)思路2的二分搜索优化
思路参考本题官方题解。
3.代码实现(Java)
//思路1————找出最大值(但不满足时间复杂度要求)
class Solution {
public int findPeakElement(int[] nums) {
int index = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] > nums[index]) {
index = i;
}
}
return index;
}
}
//思路2————迭代爬坡
class Solution {
public int findPeakElement(int[] nums) {
int length = nums.length;
//Math.random():随机选取大于等于 0.0 且小于 1.0 的伪随机 double 值
int index = (int) (Math.random() * length);
while (!(compare(nums, index - 1, index) < 0 && compare(nums, index, index + 1) > 0)) {
if (compare(nums, index, index + 1) < 0) {
index += 1;
} else {
index -= 1;
}
}
return index;
}
/* 辅助函数,输入下标 i,返回一个二元组 (0/1, nums[i]) 方便处理 nums[-1] 以及 nums[n] 的边界情况 */
public int[] get(int[] nums, int index) {
if (index == -1 || index == nums.length) {
//边界情况
return new int[]{
0, 0};
} else {
//正常情况
return new int[]{
1, nums[index]};
}
}
public int compare(int[] nums, int index1, int index2) {
int[] num1 = get(nums, index1);
int[] num2 = get(nums, index2);
if (num1[0] != num2[0]) {
return num1[0] > num2[0] ? 1 : -1;
}
if (num1[1] == num2[1]) {
return 0;
}
return num1[1] > num2[1] ? 1 : -1;
}
}
//思路3————思路2的二分搜索优化
class Solution {
public int findPeakElement(int[] nums) {
int length = nums.length;
int left = 0, right = length - 1, res = -1;
while (left <= right) {
int mid = (left + right) / 2;
if (compare(nums, mid - 1, mid) < 0 && compare(nums, mid, mid + 1) > 0) {
res = mid;
break;
}
if (compare(nums, mid, mid + 1) < 0) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return res;
}
public int[] get(int[] nums, int idx) {
if (idx == -1 || idx == nums.length) {
return new int[]{
0, 0};
}
return new int[]{
1, nums[idx]};
}
public int compare(int[] nums, int idx1, int idx2) {
int[] num1 = get(nums, idx1);
int[] num2 = get(nums, idx2);
if (num1[0] != num2[0]) {
return num1[0] > num2[0] ? 1 : -1;
}
if (num1[1] == num2[1]) {
return 0;
}
return num1[1] > num2[1] ? 1 : -1;
}
}
边栏推荐
- 【QNX Hypervisor 2.2 用户手册】4.1 构建QNX Hypervisor系统的方法
- Simple solution of regular expression
- 5G NR协议学习--TS38.211下行通道
- Google Earth engine (GEE) - quick land classification by kmeans clustering (double for loop quick parameter adjustment)
- QML学习 第二天
- Cookie和Session
- C# 35. Select default network card
- ARP protocol data processing process of neighbor subsystem
- Byte order - how to judge the big end and the small end
- Video JS library uses custom components
猜你喜欢

Byte order - how to judge the big end and the small end

UML series articles (31) architecture modeling - deployment diagram

Index in MySQL show index from XXX the meaning of each parameter

UML系列文章(30)体系结构建模---制品图

6.6 分離卷積

TinyMCE realizes automatic uploading of pasted pictures

6.6 separate convolution

conda环境下pip install 无法安装到指定conda环境中(conda环境的默认pip安装位置)

JS to load and display Excel files

A.前缀极差
随机推荐
Mysql45 lecture 01 | infrastructure: how is an SQL query executed?
ARM指令集之杂类指令
B.刷墙(C语言)
LeetCode 497. 非重叠矩形中的随机点(前缀和+二分)
字节序 - 如何判断大端小端
【深度学习基础】神经网络的学习(4)
NVIDIA Jetson Nano Developer Kit 入门
A.前缀极差
Reentrantlock source code analysis
C# 36. DataGridView line number
ARM处理器模式与寄存器
C# 37. Textbox scroll bar and multiline
Google Earth Engine(GEE)——Kmeans聚类快速进行土地分类(双for循环快速调参)
5g NR protocol learning -- ts38.211 downlink channel
Windows10 install mysql-8.0.28-winx64
PIP install in the CONDA environment cannot be installed into the specified CONDA environment (the default PIP installation location of the CONDA environment)
Reasons for SSL introduction and encryption steps
单元测试用例框架--unittest
必杀技--使用FFmpeg命令快速精准剪切视频
Shardingjdbc-5.1.0 monthly horizontal table splitting + read-write separation, automatic table creation and node table refresh