162. Find Peak Element

  • 36.3%

https://leetcode.com/problems/find-peak-element/?tab=Description

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] = -∞.

1
For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2.

Note:

Your solution should be in logarithmic complexity.


方法一:

my code:

题目要求log复杂度,这个是n复杂度,肯定是不够的。log复杂度肯定是二分搜索了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
int findPeakElement(vector<int>& nums) {
int n = nums.size();
if(n==1) return 0;
for(int i=1; i<n-1; i++)
if(nums[i]>nums[i-1] && nums[i]>nums[i+1])
return i;
if(nums[0]>nums[1])
return 0;
if(nums[n-2]<nums[n-1])
return n-1;
}
};

Sequential Search:

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
int findPeakElement(const vector<int> &num) {
for(int i = 1; i < num.size(); i ++)
{
if(num[i] < num[i-1])
{// <
return i-1;
}
}
return num.size()-1;
}
};

方法二:

8ms, 4.79%, June.21th, 2016

https://leetcode.com/discuss/17793/find-the-maximum-by-binary-search-recursion-and-iteration

Consider that each local maximum is one valid peak.
My solution is to find one local maximum with binary search.
Binary search satisfies the O(logn) computational complexity.

Binary Search: recursion

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:

int findPeakElement(const vector<int> &num) {
return Helper(num, 0, num.size()-1);
}
int Helper(const vector<int> &num, int low, int high)
{
if(low == high)
return low;
else
{
int mid1 = (low+high)/2;
int mid2 = mid1+1;
if(num[mid1] > num[mid2])
return Helper(num, low, mid1);
else
return Helper(num, mid2, high);
}
}
};

方法三:

Binary Search: iteration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
int findPeakElement(const vector<int> &num)
{
int low = 0;
int high = num.size()-1;

while(low < high)
{
int mid1 = (low+high)/2;
int mid2 = mid1+1;
if(num[mid1] < num[mid2])
low = mid2;
else
high = mid1;
}
return low;
}
};

cpp

Solution 1:

8ms, 4.79%, June.21th, 2016

https://leetcode.com/discuss/17793/find-the-maximum-by-binary-search-recursion-and-iteration

1
2
3
4
5
6
7
8
9
class Solution {
public:
int findPeakElement(vector<int>& nums) {
for(int i = 1; i < nums.size(); i++)
if(nums[i] < nums[i-1])
return i-1;
return nums.size() - 1;
}
};

Solution 2:

4ms, 84.77%, June.21th, 2016

https://leetcode.com/discuss/17793/find-the-maximum-by-binary-search-recursion-and-iteration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
int findPeakElement(vector<int>& nums) {
return Helper(nums, 0, nums.size()-1);
}

int Helper(const vector<int> &nums, int low, int high){
if(low == high)
return low;
else{
int mid1 = (low + high) / 2;
int mid2 = mid1 + 1;
if(nums[mid1] > nums[mid2])
return Helper(nums, low, mid1);
else
return Helper(nums, mid2, high);
}
}
};

Solution 3:

python

Solution 1:

48ms, 82.40%, June.21th, 2016

https://leetcode.com/discuss/35744/my-clean-and-readable-python-solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution(object):
def findPeakElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
left = 0
right = len(nums)-1

while left < right:
mid = (left+right)/2
if nums[mid] > nums[mid+1] and nums[mid] > nums[mid-1]:
return mid

if nums[mid] < nums[mid+1]:
left = mid+1
else:
right = mid-1

return left

java

Solution 1:

0ms, 33.78%, June.21th, 2016

https://leetcode.com/discuss/18107/o-logn-solution-javacode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class Solution {
public int findPeakElement(int[] num) {
return helper(num,0,num.length-1);
}

public int helper(int[] num,int start,int end){
if(start == end){
return start;
}else if(start+1 == end){
if(num[start] > num[end]) return start;
return end;
}else{

int m = (start+end)/2;

if(num[m] > num[m-1] && num[m] > num[m+1]){

return m;

}else if(num[m-1] > num[m] && num[m] > num[m+1]){

return helper(num,start,m-1);

}else{

return helper(num,m+1,end);

}

}
}
}
谢谢你,可爱的朋友。