162. Find Peak Element

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 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)
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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值