当前位置:网站首页>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))
};
边栏推荐
- LeetCode:236. 二叉树的最近公共祖先
- 超高效!Swagger-Yapi的秘密
- Philosophical enlightenment from single point to distributed
- SAP ui5 date type sap ui. model. type. Analysis of the parsing format of date
- 【嵌入式】使用JLINK RTT打印log
- LeetCode:836. 矩形重叠
- ant-design的走马灯(Carousel)组件在TS(typescript)环境中调用prev以及next方法
- [embedded] cortex m4f DSP Library
- 力扣每日一题(二)
- [OC]-<UI入门>--常用控件的学习
猜你喜欢

Esp8266-rtos IOT development
![[embedded] cortex m4f DSP Library](/img/83/ab421d5cc18e907056ec2bdaeb7d5c.png)
[embedded] cortex m4f DSP Library

Roguelike game into crack the hardest hit areas, how to break the bureau?

同一局域网的手机和电脑相互访问,IIS设置

swagger设置字段required必填

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

Export IEEE document format using latex

Detailed explanation of dynamic planning

Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges

Computer graduation design PHP Zhiduo online learning platform
随机推荐
LeetCode:673. 最长递增子序列的个数
Marathon envs project environment configuration (strengthen learning and imitate reference actions)
Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
广州推进儿童友好城市建设,将探索学校周边200米设安全区域
Problems in loading and saving pytorch trained models
LeetCode:236. The nearest common ancestor of binary tree
如何进行接口测试测?有哪些注意事项?保姆级解读
TP-LINK 企业路由器 PPTP 配置
Promise 在uniapp的简单使用
Li Kou daily question 1 (2)
Light of domestic games destroyed by cracking
Charging interface docking tutorial of enterprise and micro service provider platform
C语言深度解剖——C语言关键字
多元聚类分析
Pytorch view tensor memory size
UML圖記憶技巧
企微服务商平台收费接口对接教程
角色动画(Character Animation)的现状与趋势
hutool优雅解析URL链接并获取参数
深度剖析C语言数据在内存中的存储