A peak element is an element that is greater than its neighbors.
Given an input array where num[i] ≠ num[i+1]
, find a peak element and return its index.
The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.
You may imagine that num[-1] = num[n] = -∞
.
For example, in array [1, 2, 3, 1]
, 3 is a peak element and your function should return the index number 2.
Your solution should be in logarithmic(对数的) complexity.
我用了很直观的方法,但是不满足 O(logN)。
public int findPeakElement(int[] nums) {
int n=nums.length;
if(n==1){
return 0;
}
int leftNeighbour=Integer.MIN_VALUE;
int rightNeighbour=-1;
for(int i=0;i<=n-2;i++){
rightNeighbour=nums[i+1];
if(nums[i]>leftNeighbour&&nums[i]>rightNeighbour){
return i;
}
leftNeighbour=nums[i];
}
if(leftNeighbour<nums[n-1]){
return n-1;
}
return -1;
}
让我们来看看大神们的方法。说到 O(logN) 时间,不得不想到用二分查找法。
注意:数组中有可能有很多个peak,而我们只需要找到其中一个peak即可。
If num[i-1] < num[i] > num[i+1], then num[i] is peak
If num[i-1] < num[i] < num[i+1], then num[i+1...n-1] must contains a peak
If num[i-1] > num[i] > num[i+1], then num[0...i-1] must contains a peak
If num[i-1] > num[i] < num[i+1], then both sides have peak
(n is num.length)
public int findPeakElement(int[] nums) {
int N = nums.length;
if (N == 1) {
return 0;
}
int left = 0, right = N - 1;
while (right - left > 1) {
int mid = left + (right - left) / 2;
if (nums[mid] < nums[mid + 1]) {
left = mid + 1;
} else {
right = mid;
}
}
return (left == N - 1 || nums[left] > nums[left + 1]) ? left : right;
}
这道题有 solutions:https://leetcode.com/problems/find-peak-element/solution/
Solution
Approach #1 Linear Scan [Accepted]
Java
public class Solution { public int findPeakElement(int[] nums) { for (int i = 0; i < nums.length - 1; i++) { if (nums[i] > nums[i + 1]) return i; } return nums.length - 1; } }
Complexity Analysis
-
Time complexity : O(n). We traverse the nums array of size n once only.
-
Space complexity : O(1). Constant extra space is used.
Approach #2 Recursive Binary Search [Accepted]
Java
public class Solution { public int findPeakElement(int[] nums) { return search(nums, 0, nums.length - 1); } public int search(int[] nums, int l, int r) { if (l == r) return l; int mid = (l + r) / 2; if (nums[mid] > nums[mid + 1]) return search(nums, l, mid); return search(nums, mid + 1, r); } }
Complexity Analysis
-
Time complexity : O(log2(n)). We reduce the search space in half at every step. Thus, the total search space will be consumed in log2(n) steps. Here, n refers to the size of nums array.
-
Space complexity : O(log2(n)). We reduce the search space in half at every step. Thus, the total search space will be consumed in log2(n) steps. Thus, the depth of recursion tree will go upto log2(n).
Approach #3 Iterative Binary Search [Accepted]
Java
public class Solution { public int findPeakElement(int[] nums) { int l = 0, r = nums.length - 1; while (l < r) { int mid = (l + r) / 2; if (nums[mid] > nums[mid + 1]) r = mid; else l = mid + 1; } return l; } }
Complexity Analysis
-
Time complexity : O(log2(n)). We reduce the search space in half at every step. Thus, the total search space will be consumed in log2(n) steps. Here, n refers to the size of nums array.
-
Space complexity : O(1). Constant extra space is used.