LeetCode_二分搜索_中等_162. 寻找峰值

x33g5p2x  于2022-06-13 转载在 其他  
字(2.2k)|赞(0)|评价(0)|浏览(187)

1.题目

峰值元素是指其值严格大于左右相邻值的元素。

给你一个整数数组 nums,找到峰值元素并返回其索引。数组可能包含多个峰值,在这种情况下,返回任何一个峰值所在位置即可

你可以假设 nums[-1] = nums[n] = -∞ 。

你必须实现时间复杂度为 O(log n) 的算法来解决此问题。

示例 1:

输入:nums = [1,2,3,1]
输出:2
解释:3 是峰值元素,你的函数应该返回其索引 2。

示例 2:
输入:nums = [1,2,1,3,5,6,4]
输出:1 或 5
解释:你的函数可以返回索引 1,其峰值元素为 2;或者返回索引 5, 其峰值元素为 6。

提示:
1 <= nums.length <= 1000
-231 <= nums[i] <= 231 - 1
对于所有有效的 i 都有 nums[i] != nums[i + 1]

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/find-peak-element

2.思路

(1)找出最大值(但不满足时间复杂度要求)
由于题目保证了对于所有有效的 i 都有 nums[i] != nums[i + 1],那么数组 nums 中最大值两侧的元素一定严格小于最大值本身。因此,最大值所在的位置就是一个可行的峰值位置。所以只需对数组 nums 进行一次遍历,找到最大值对应对应的下标即可。不过该方法的时间复杂度为 O(n),并不满足题目的要求。

(2)迭代爬坡
思路参考本题官方题解

(3)思路2的二分搜索优化
思路参考本题官方题解

3.代码实现(Java)

//思路1————找出最大值(但不满足时间复杂度要求)
class Solution {
    public int findPeakElement(int[] nums) {
        int index = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > nums[index]) {
                index = i;
            }
        }
        return index;
    }
}
//思路2————迭代爬坡
class Solution {
    public int findPeakElement(int[] nums) {
        int length = nums.length;
        //Math.random():随机选取大于等于 0.0 且小于 1.0 的伪随机 double 值
        int index = (int) (Math.random() * length);
        while (!(compare(nums, index - 1, index) < 0 && compare(nums, index, index + 1) > 0)) {
            if (compare(nums, index, index + 1) < 0) {
                index += 1;
            } else {
                index -= 1;
            }
        }
        return index;
    }
    
    /*
        辅助函数,输入下标 i,返回一个二元组 (0/1, nums[i])
        方便处理 nums[-1] 以及 nums[n] 的边界情况
    */
    public int[] get(int[] nums, int index) {
        if (index == -1 || index == nums.length) {
            //边界情况
            return new int[]{0, 0};
        } else {
            //正常情况
            return new int[]{1, nums[index]};
        }
    }
    
    public int compare(int[] nums, int index1, int index2) {
        int[] num1 = get(nums, index1);
        int[] num2 = get(nums, index2);
        if (num1[0] != num2[0]) {
            return num1[0] > num2[0] ? 1 : -1;
        }
        if (num1[1] == num2[1]) {
            return 0;
        }
        return num1[1] > num2[1] ? 1 : -1;
    }
}
//思路3————思路2的二分搜索优化
class Solution {
    public int findPeakElement(int[] nums) {
        int length = nums.length;
        int left = 0, right = length - 1, res = -1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (compare(nums, mid - 1, mid) < 0 && compare(nums, mid, mid + 1) > 0) {
                res = mid;
                break;
            }
            if (compare(nums, mid, mid + 1) < 0) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return res;
    }

    public int[] get(int[] nums, int idx) {
        if (idx == -1 || idx == nums.length) {
            return new int[]{0, 0};
        }
        return new int[]{1, nums[idx]};
    }

    public int compare(int[] nums, int idx1, int idx2) {
        int[] num1 = get(nums, idx1);
        int[] num2 = get(nums, idx2);
        if (num1[0] != num2[0]) {
            return num1[0] > num2[0] ? 1 : -1;
        }
        if (num1[1] == num2[1]) {
            return 0;
        }
        return num1[1] > num2[1] ? 1 : -1;
    }
}

相关文章

微信公众号

最新文章

更多