当前位置:网站首页>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)
边栏推荐
- Softmax regression (pytorch)
- Rewrite Boston house price forecast task (using paddlepaddlepaddle)
- C#项目-寝室管理系统(1)
- Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported
- 深度学习入门之线性回归(PyTorch)
- Are there any other high imitation projects
- Raspberry pie 4B deploys lnmp+tor and builds a website on dark web
- Ut2017 learning notes
- Hands on deep learning pytorch version exercise answer - 2.2 preliminary knowledge / data preprocessing
- Leetcode刷题---1385
猜你喜欢

High imitation bosom friend manke comic app

Secure in mysql8.0 under Windows_ file_ Priv is null solution

波士顿房价预测(TensorFlow2.9实践)

ECMAScript -- "ES6 syntax specification # Day1

Knowledge map reasoning -- hybrid neural network and distributed representation reasoning

Ind wks first week

Multilayer perceptron (pytorch)

Leetcode - 1172 plate stack (Design - list + small top pile + stack))

Raspberry pie 4B deploys lnmp+tor and builds a website on dark web

Hands on deep learning pytorch version exercise solution - 2.4 calculus
随机推荐
Raspberry pie 4B deploys lnmp+tor and builds a website on dark web
丢弃法Dropout(Pytorch)
Leetcode刷题---35
C#项目-寝室管理系统(1)
一步教你溯源【钓鱼邮件】的IP地址
Leetcode - 705 design hash set (Design)
Softmax regression (pytorch)
[C question set] of Ⅵ
Adaptive Propagation Graph Convolutional Network
Leetcode skimming ---10
神经网络入门之预备知识(PyTorch)
Leetcode刷题---1385
What did I read in order to understand the to do list
What can I do to exit the current operation and confirm it twice?
Multi-Task Feature Learning for Knowledge Graph Enhanced Recommendation
Codeup: word replacement
ECMAScript -- "ES6 syntax specification # Day1
Ind FHL first week
二分查找法
Knowledge map reasoning -- hybrid neural network and distributed representation reasoning