当前位置:网站首页>LeetCode:162. Looking for peak
LeetCode:162. Looking for peak
2022-07-06 08:51:00 【Bertil】
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 Any peak Just where you are .
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
- -2^31 <= nums[i] <= 2^31 - 1
- For all that works i There are nums[i] != nums[i + 1]
Their thinking
1. First, find the largest element , Then return to its index
2. explain : Why is the maximum the answer ?
- Because the title has assumed nums[-1] = nums[n] = -∞, So the peak will not appear in the first and last elements
- The left and right adjacent elements of the maximum value in the array must be less than the maximum value
Code
/** * @param {number[]} nums * @return {number} */
var findPeakElement = function(nums) {
return nums.indexOf(Math.max(...nums))
};
边栏推荐
- 可变长参数
- R language ggplot2 visualization, custom ggplot2 visualization image legend background color of legend
- TP-LINK enterprise router PPTP configuration
- Restful API design specification
- Super efficient! The secret of swagger Yapi
- Revit secondary development Hof method calls transaction
- 数学建模2004B题(输电问题)
- Deep analysis of C language pointer
- Deep anatomy of C language -- C language keywords
- C語言雙指針——經典題型
猜你喜欢

Promise 在uniapp的简单使用

ROS compilation calls the third-party dynamic library (xxx.so)

Sublime text using ctrl+b to run another program without closing other runs

LeetCode:124. 二叉树中的最大路径和

ant-design的走马灯(Carousel)组件在TS(typescript)环境中调用prev以及next方法

Warning in install. packages : package ‘RGtk2’ is not available for this version of R

UML diagram memory skills

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

Delay initialization and sealing classes

Image, CV2 read the conversion and size resize change of numpy array of pictures
随机推荐
UML图记忆技巧
角色动画(Character Animation)的现状与趋势
LeetCode:394. 字符串解码
Pytorch view tensor memory size
Roguelike game into crack the hardest hit areas, how to break the bureau?
Leetcode刷题题解2.1.1
Swagger setting field required is mandatory
ESP8266-RTOS物联网开发
The network model established by torch is displayed by torch viz
LeetCode:673. 最长递增子序列的个数
C语言深度解剖——C语言关键字
TDengine 社区问题双周精选 | 第三期
Leetcode: Sword Finger offer 42. Somme maximale des sous - tableaux consécutifs
深度剖析C语言数据在内存中的存储
Image, CV2 read the conversion and size resize change of numpy array of pictures
ROS compilation calls the third-party dynamic library (xxx.so)
[OC]-<UI入门>--常用控件-提示对话框 And 等待提示器(圈)
Hutool gracefully parses URL links and obtains parameters
Unsupported operation exception
pytorch查看张量占用内存大小