当前位置:网站首页>Leetcode 162 Looking for peak value (June 11, 2022)
Leetcode 162 Looking for peak value (June 11, 2022)
2022-06-12 10:47:00 【ChaoYue_ miku】
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
-231 <= nums[i] <= 231 - 1
For all that works i There are nums[i] != nums[i + 1]
Pass times 232,819 Submit the number 470,577
source : Power button (LeetCode)
link :https://leetcode.cn/problems/find-peak-element
Method 1 : Two points search
C++ Submission :
class Solution {
public:
int findPeakElement(vector<int>& nums) {
int n = nums.size();
auto get = [=](int i) -> pair<int, int>{
if(i == -1 || i == n){
return {
0, 0};
}else{
return {
1, nums[i]};
}
};
int l = 0, r = n, index = 0;
while(l <= r){
int mid = (l + r) / 2;
if(get(mid - 1) < get(mid) && get(mid) > get(mid + 1)){
index = mid;
break;
}
if(get(mid) < get(mid + 1)){
l = mid + 1;
}else{
r = mid - 1;
}
}
return index;
}
};
边栏推荐
- M-arch (fanwai 12) gd32l233 evaluation -cau encryption and decryption (tease Xiaobian)
- PHP: Excel to get the letter header
- How to play the 2022 Taobao 618 Super Cat Games? What are the strategies for the Super Cat Games
- PHP specifies the number of people to distribute the specified amount equally at random (scaling method)
- Golang start service background daemon
- NFT数字藏品的可验证性和稀缺性
- JS obtains the time period of this week and last week (one time period is from Monday to Sunday)
- Malicious code analysis practice - lab03-03 Exe basic dynamic analysis
- Get array median
- AcWing 135. Maximum subsequence sum (prefix sum + monotone queue to find the minimum value of fixed length interval)
猜你喜欢

【机器学习】基于鸢尾花(iris)数据集的逻辑回归分类实践

Malicious code analysis practice - lab03-01 Exe basic dynamic analysis

浅谈调和形状上下文特征HSC对3DSC的改进

Set SVG color

Pseudo static setting of access database in win2008 R2 iis7.5

淺談調和形狀上下文特征HSC對3DSC的改進

Global and local existence of array, integer and character variables

Leetcode 2169. Get operands of 0

SOT23(Small Outline Transistor)

2022淘宝618超级喵运会玩法攻略 618超级喵运会玩法技巧
随机推荐
基于QT的旅行查询与模拟系统
M-Arch(番外13)GD32L233评测-来点音乐
DrQueueOnRails 集成 LDAP 验证
淘宝618超级喵运会怎么玩?超级喵运会整体活动攻略来了
元宇宙链游与传统游戏的区别
Collation of common functions in JS
Mysql5.6.24 installation free deployment method
Stream as a return value in WCF - who disposes of it- Stream as a return value in WCF - who disposes it?
PHP uses leanclound to save associated properties
Composer command
Introduction to encoding formats (ASCII, Unicode and UTF-8)
重量级代理缓存服务器Squid
AcWing 131. The largest rectangle in the histogram (monotone stack classic application template)
FPGA key experiment
AcWing 131. 直方图中最大的矩形(单调栈经典运用 模板)
Php中redis的keys问题
Pseudo static setting of access database in win2008 R2 iis7.5
Php:redis uses geospatial
JS scale down the width and height of the picture
k58.第一章 基于kubeadm安装kubernetes v1.23 -- 集群部署