leetcode 162. Find Peak Element

257 篇文章 17 订阅

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.

如果有多个peak点,只要返回其中任意一个peak点的索引即可了。有意思的是 要求我们在 O(logN) 时间内完成。

我用了很直观的方法,但是不满足 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)O(n). We traverse the numsnums array of size nn once only.

  • Space complexity : O(1)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\big(log_2(n)\big)O(log2(n)). We reduce the search space in half at every step. Thus, the total search space will be consumed in log_2(n)log2(n) steps. Here, nn refers to the size of numsnums array.

  • Space complexity : O\big(log_2(n)\big)O(log2(n)). We reduce the search space in half at every step. Thus, the total search space will be consumed in log_2(n)log2(n) steps. Thus, the depth of recursion tree will go upto log_2(n)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\big(log_2(n)\big)O(log2(n)). We reduce the search space in half at every step. Thus, the total search space will be consumed in log_2(n)log2(n) steps. Here, nn refers to the size of numsnums array.

  • Space complexity : O(1)O(1). Constant extra space is used.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值