当前位置:网站首页>Bm19 looking for peak
Bm19 looking for peak
2022-06-21 17:37:00 【Programmer Xiao Li】
Given a length of n Array of nums, Please find the peak and return its index . The array may contain multiple peaks , under these circumstances , Just return to any location .

public int findPeakElement (int[] nums) {
// write code here
if (nums == null || nums.length == 0){
return -1;
}
int i = 0;
while (i <= nums.length - 1){
boolean left = false;
boolean right = false;
if (i - 1 < 0 || nums[i] > nums[i-1]){
left = true;
}
if (i + 1 > nums.length - 1 || nums[i] > nums[i+1]){
right = true;
}
if (left && right){
return i;
}
i++;
}
return -1;
}notes : Direct judgment nums[i] > nums[i-1] && nums[i] > nums[i+1] There may be only one or two cases left out .
Additional conditions :
1. The peak element refers to the element whose value is strictly greater than the left and right adjacent values . Strictly greater than means that there can be no equal to
2. hypothesis nums[-1] = nums[n] = −∞
3. For all that works i There are nums[i] != nums[i + 1]
4. You can use O(logN) Time complexity to achieve this problem ?
The principle of using dichotomy is :
Use dichotomy , If there is a certain location nums[i] < nums[i+1] when , Then continue to look right , There must be an extreme value on the right .
( Because the left side mid~mid+1 An ascending sequence has been completed , that , There is only one descending order on the right , The extreme value appears , If the right side is the green value in the figure below , that mid+1 It's extreme value , If it's red , Keep looking right , Until the last element at the latest , It must be the limit value , Why? ? All the way to the right , Indicates that the left side is incremented , and nums[n] = −∞, So it must be satisfied ) The same is true for the left side .

public int findPeakElement (int[] nums) {
// write code here
if (nums == null || nums.length == 0){
return -1;
}
int low = 0;
int high = nums.length - 1;
while (low < high){
int mid = low + (high - low) / 2;
if (nums[mid] < nums[mid + 1]){
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
边栏推荐
- Still using xshell? Try this cool SSH terminal tool, which is very powerful!
- The main relations and differences between Poisson sampling and Bernoulli sampling
- 自然科学的根本任务
- 类、接口、函数
- MySQL 1055错误-this is incompatible with sql_mode=only_full_group_by解决方案
- Kotlin常用函数 let,with,apply,also,run
- 正则表达式
- 全国行政区划
- 20 pyGame module to make a jumping ball game
- BM22 比较版本号
猜你喜欢

Matlab中xticks函数

Still using xshell? Try this cool SSH terminal tool, which is very powerful!
![[MySQL learning notes 19] multi table query](/img/e6/4cfb8772e14bc82c4fdc9ad313e58a.png)
[MySQL learning notes 19] multi table query

The next stop of Intelligent Manufacturing: cloud native + edge computing two wheel drive

Vector data download for mainland and global epidemic data, based on geo JSON to SHP

BM95 分糖果问题
![[MySQL learning notes 15] user management](/img/e8/4773f99c42891789b0311506b8384f.png)
[MySQL learning notes 15] user management

module.exports指向问题

data type

Bm95 points candy problem
随机推荐
Four areas of telephone memory
data type
variable
QT knowledge: using the qgraphicspixmapitem class
Nanjing University static program analyses -- Introduction learning notes
BM95 分糖果问题
【数据集】|BigDetection
[MySQL learning notes 18] constraints
硅橡胶玻纤管EN45545防火试验的难易程度
Android kotlin class delegation by, by lazy key
The beta version of move protocol is stable, and it is temporarily decided to expand the scale of the prize pool
[MySQL learning notes 11] sort query
PTA l3-031 thousand hand Guanyin (30 points)
【mysql学习笔记11】排序查询
第八章 可编程接口芯片及应用【微机原理】
钣金行业MES系统的特色需求
BM23 二叉树的前序遍历
Characteristic requirements of MES system in sheet metal industry
Week 13 summary blog (week 15 of the school calendar) dynamic planning summary
焱融科技 YRCloudFile 与安腾普完成兼容认证,共创存储新蓝图