当前位置:网站首页>Leetcode skimming ---852
Leetcode skimming ---852
2022-07-03 10:35:00 【Long time no see 0327】
subject : Arrays that match the following properties arr be called Mountains array :
- arr.length >= 3
- There is i(0 < i < arr.length - 1) bring :
- arr[0] < arr[1] < ... arr[i-1] < arr[i]
- arr[i] > arr[i+1] > ... > arr[arr.length - 1]
Here's an array of mountains made up of integers arr , Return anything that satisfies arr[0] < arr[1] < ... arr[i - 1] < arr[i] > arr[i + 1] > ... > arr[arr.length - 1] The subscript i .
Input :arr = [0,2,1,0]
Output :1
Method 1 : enumeration
class Solution {
public:
int peakIndexInMountainArray(vector<int>& arr) {
int n = arr.size();
int ans = -1;
for (int i = 1; i < n - 1; ++i) {
if (arr[i] > arr[i + 1]) {
ans = i;
break;
}
}
return ans;
}
};Complexity analysis
Time complexity :O(n)
Spatial complexity :O(1)
Method 2 : Two points search
class Solution {
public:
int peakIndexInMountainArray(vector<int>& arr) {
int n = arr.size();
int left = 1, right = n - 2, ans = 0;
while (left <= right) {
int mid = (left + right) / 2;
if (arr[mid] > arr[mid + 1]) {
ans = mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
return ans;
}
};Complexity analysis
Time complexity :O(logn)
Spatial complexity :O(1)
边栏推荐
- 七、MySQL之数据定义语言(二)
- Ut2014 learning notes
- Neural Network Fundamentals (1)
- Powshell's set location: unable to find a solution to the problem of accepting actual parameters
- Multilayer perceptron (pytorch)
- I really want to be a girl. The first step of programming is to wear women's clothes
- Ind kwf first week
- Leetcode skimming ---263
- LeetCode - 900. RLE iterator
- Out of the box high color background system
猜你喜欢

Policy gradient Method of Deep Reinforcement learning (Part One)

An open source OA office automation system

Softmax 回归(PyTorch)

Policy Gradient Methods of Deep Reinforcement Learning (Part Two)

Ut2012 learning notes

一个30岁的测试员无比挣扎的故事,连躺平都是奢望

Leetcode - 705 design hash set (Design)

A complete mall system

Standard library header file

Raspberry pie 4B installs yolov5 to achieve real-time target detection
随机推荐
【SQL】一篇带你掌握SQL数据库的查询与修改相关操作
Policy gradient Method of Deep Reinforcement learning (Part One)
CSDN, I'm coming!
LeetCode - 900. RLE iterator
一步教你溯源【钓鱼邮件】的IP地址
Preliminary knowledge of Neural Network Introduction (pytorch)
Leetcode刷题---202
The imitation of jd.com e-commerce project is coming
丢弃法Dropout(Pytorch)
Ind wks first week
QT creator uses OpenCV Pro add
Julia1.0
Leetcode - 706 design hash mapping (Design)*
实战篇:Oracle 数据库标准版(SE)转换为企业版(EE)
Leetcode刷题---1
Content type ‘application/x-www-form-urlencoded; charset=UTF-8‘ not supported
Model evaluation and selection
Hands on deep learning pytorch version exercise solution-3.3 simple implementation of linear regression
Leetcode刷题---44
神经网络入门之矩阵计算(Pytorch)