当前位置:网站首页>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)
边栏推荐
- Policy Gradient Methods of Deep Reinforcement Learning (Part Two)
- Hands on deep learning pytorch version exercise solution-3.3 simple implementation of linear regression
- 一步教你溯源【钓鱼邮件】的IP地址
- What can I do to exit the current operation and confirm it twice?
- Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported
- Boston house price forecast (tensorflow2.9 practice)
- High imitation wechat
- Leetcode刷题---852
- Step 1: teach you to trace the IP address of [phishing email]
- GAOFAN Weibo app
猜你喜欢
LeetCode - 715. Range module (TreeSet)*****
Step 1: teach you to trace the IP address of [phishing email]
安装yolov3(Anaconda)
Hands on deep learning pytorch version exercise answer - 2.2 preliminary knowledge / data preprocessing
Adaptive Propagation Graph Convolutional Network
Training effects of different data sets (yolov5)
A complete mall system
7、 Data definition language of MySQL (2)
Hands on deep learning pytorch version exercise solution-3.3 simple implementation of linear regression
[LZY learning notes -dive into deep learning] math preparation 2.5-2.7
随机推荐
LeetCode - 900. RLE iterator
Leetcode刷题---367
An open source OA office automation system
Hands on deep learning pytorch version exercise solution - 2.5 automatic differentiation
What useful materials have I learned from when installing QT
Ind FHL first week
QT creator uses OpenCV Pro add
Jetson TX2 刷机
Preliminary knowledge of Neural Network Introduction (pytorch)
20220610 other: Task Scheduler
20220607 others: sum of two integers
Leetcode - 706 design hash mapping (Design)*
Data classification: support vector machine
Multilayer perceptron (pytorch)
[LZY learning notes dive into deep learning] 3.1-3.3 principle and implementation of linear regression
Leetcode刷题---44
Standard library header file
[graduation season] the picture is rich, and frugality is easy; Never forget chaos and danger in peace.
MySQL报错“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”解决方法
20220603 Mathematics: pow (x, n)