当前位置:网站首页>LeetCode_ Binary search_ Medium_ 162. looking for peaks
LeetCode_ Binary search_ Medium_ 162. looking for peaks
2022-06-12 11:56:00 【I've been up and down in the Jianghu】
1. subject
The peak element refers to the element whose value is strictly greater than the left and right adjacent values .
Give you an array of integers nums, Find the peak element and return its index . The array may contain multiple peaks , under these circumstances , Return to any peak position .
You can assume nums[-1] = nums[n] = -∞ .
You must achieve a time complexity of O(log n) Algorithm to solve this problem .
Example 1:
Input :nums = [1,2,3,1]
Output :2
explain :3 Is the peak element , Your function should return its index 2.
Example 2:
Input :nums = [1,2,1,3,5,6,4]
Output :1 or 5
explain : Your function can return the index 1, Its peak element is 2; Or return index 5, Its peak element is 6.
Tips :
1 <= nums.length <= 1000
-231 <= nums[i] <= 231 - 1
For all that works i There are nums[i] != nums[i + 1]
source : Power button (LeetCode)
link :https://leetcode.cn/problems/find-peak-element
2. Ideas
(1) Find the maximum ( But it does not meet the time complexity requirements )
Because the title guarantees for all valid i There are nums[i] != nums[i + 1], So array nums The elements on both sides of the maximum must be strictly smaller than the maximum itself . therefore , The position of the maximum value is a feasible peak position . So just do the array nums Do a walk , Find the subscript corresponding to the maximum value . However, the time complexity of this method is O(n), Does not meet the requirements of the topic .
(2) Iterative climbing
Train of thought reference Official solution to this problem .
(3) Ideas 2 Binary search optimization based on
Train of thought reference Official solution to this problem .
3. Code implementation (Java)
// Ideas 1———— Find the maximum ( But it does not meet the time complexity requirements )
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;
}
}
// Ideas 2———— Iterative climbing
class Solution {
public int findPeakElement(int[] nums) {
int length = nums.length;
//Math.random(): Randomly select greater than or equal to 0.0 And less than 1.0 Pseudorandom of double value
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;
}
/* Auxiliary function , Input subscript i, Returns a tuple (0/1, nums[i]) Easy to handle nums[-1] as well as nums[n] The situation of the border */
public int[] get(int[] nums, int index) {
if (index == -1 || index == nums.length) {
// Boundary situation
return new int[]{
0, 0};
} else {
// Normal condition
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;
}
}
// Ideas 3———— Ideas 2 Binary search optimization based on
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;
}
}
边栏推荐
- 5g NR protocol learning -- ts38.211 downlink channel
- K53. Chapter 2 installing kubernetes v1.22 based on binary packages -- cluster deployment
- Basic concepts of machine learning
- LeetCode 497. 非重叠矩形中的随机点(前缀和+二分)
- Video JS library uses custom components
- TinyMCE series (III) introduction to common TinyMCE APIs
- Architecture training module 7
- Inter class and intra class relations in video classification -- regularization
- 邻居子系统之邻居项状态更新
- Tpage design
猜你喜欢

6.6 Convolution de séparation

Relation entre les classes et à l'intérieur des classes de classification vidéo - - Régularisation

ARM指令集之批量Load/Store指令

TinyMCE series (II) TinyMCE plug-in development

C# 35. Select default network card

5G NR协议学习--TS38.211下行通道

Node crawler puppeter usage

視頻分類的類間和類內關系——正則化

ARM处理器模式与寄存器

LeetCode 497. 非重叠矩形中的随机点(前缀和+二分)
随机推荐
Go sends SMS based on Tencent cloud
Logrotate log rotation method create and copyruncate principles
Reentrantlock source code analysis
6.6 分离卷积
Basic principle of Doppler effect
Go sends SMS based on alicloud
MySQL - built in function
Inter class and intra class relations in video classification -- regularization
Spark常用封装类
PDSCH related
ARM处理器模式与寄存器
LeetCode 890. Find and replace mode (analog + double hash table)
无重复字符的最长字符串(LeetCode 3)
Design of virtual scrolling list
K53. Chapter 2 installing kubernetes v1.22 based on binary packages -- cluster deployment
必杀技--使用FFmpeg命令快速精准剪切视频
Node crawler puppeter usage
Load/store memory access instruction of arm instruction set (1)
Byte order (network / host) conversion
Design of TTable