162. Find Peak Element

来源:互联网 发布:网络心理咨询师招聘 编辑:程序博客网 时间:2024/04/29 20:14

Question

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.

click to show spoilers.

Note:
Your solution should be in logarithmic complexity.

THOUGHT I(直接破解)

从头开始遍历,保留前位指针、中位指针和后位指针,然后比较他们的大小,满足题意要求后输出。

代码
public class Solution {    public int findPeakElement(int[] nums) {        int fore = 2,mid = 1,last = 0;        if(nums.length == 0)            return -1;        else if(nums.length == 1)            return 0;        else if(nums.length == 2){            if(nums[0] > nums[1])                return 0;            else                return 1;        }        else{            if(nums[0] > nums[1])                return 0;            else if(nums[nums.length - 1] > nums[nums.length - 2])                return nums.length - 1;            else{                for(;fore < nums.length;fore++){                if(nums[mid] >= nums[fore] && nums[mid] >= nums[last])                    return mid;                last = mid;                mid = fore;                }            }        }        return 0;    }}
结果及分析

时间复杂度为O(N),空间复杂度为O(1)。特别需要注意的情况,当只有一个输入的时候,两个输入的时候。还有就是当边界值大于它仅有的一个邻居的时候。根据题意只要大于它的邻居就可以,可以分为0个邻居,一个邻居,两个邻居,其中一个邻居的情况有两种,一是只有两个元素,二是边界元素。不过这种解法不能满足题意的要求。

THOUGHT II

借鉴二分法的思想,This problem is similar to Local Minimum. And according to the given condition, num[i] != num[i+1], there must exist a O(logN) solution. So we use binary search for this problem.

If num[i-1] < num[i] > num[i+1], then num[i] is peakIf num[i-1] < num[i] < num[i+1], then num[i+1...n-1] must contains a peakIf num[i-1] > num[i] > num[i+1], then num[0...i-1] must contains a peakIf num[i-1] > num[i] < num[i+1], then both sides have peak (n is num.length)
Here is the code
public class Solution {//借鉴二分查找的思想    public int findPeakElement(int[] nums) {         return find_peak(nums,0,nums.length - 1);    }    public int find_peak(int[] nums,int start,int end){        if(start == end)            return start;        else if(start + 1 == end){            if(nums[start] > nums[end])                 return start;            else                 return end;        }        else{            int m = (start + end)/2;            if(nums[m - 1] < nums[m]&&nums[m] > nums[m + 1])                return m;            else if(nums[m -1] < nums[m]&&nums[m] < nums[m + 1])                return find_peak(nums,m + 1,end);            else                return find_peak(nums,start,m - 1);        }    }}
结果及分析

符合题意时间复杂度为o(logn)。基本上是最优的解法,二分查找的思想,先确定中间是否符合,然后能否确定在两边存在。这道题不容易想到二分法,但是o(logn)一定要想到二分法。

二刷THOUGHT III

由于nums[-1]被看做是负无穷,所以nums[0]>nums[-1],若nums[1]

CODE
public class Solution {    public int findPeakElement(int[] nums) {        int n = nums.length;        for(int i = 1;i < n;i++){            if(nums[i] < nums[i - 1])                return i - 1;        }        return n - 1;    }}

RESULT

time complexity is O(n)

0 0
原创粉丝点击