• 【LeetCode-162】寻找峰值


    10.6 寻找峰值【162】

    10.6.1 题目描述

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

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

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

    你必须实现时间复杂度为 O(log n) 的算法来解决此问题。
    在这里插入图片描述

    10.6.2 方法一:寻找最大值

    思路与算法

    由于题目保证了 nums [ i ] ≠ nums [ i + 1 ] \textit{nums}[i] \neq \textit{nums}[i+1] nums[i]=nums[i+1],那么数组 nums 中最大值两侧的元素一定严格小于最大值本身。因此,最大值所在的位置就是一个可行的峰值位置。

    我们对数组 nums 进行一次遍历,找到最大值对应的位置即可。

    代码

    class Solution {
        public int findPeakElement(int[] nums) {
            int idx = 0;
            for (int i = 1; i < nums.length; ++i) {
                if (nums[i] > nums[idx]) {
                    idx = i;
                }
            }
            return idx;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    复杂度分析

    • 时间复杂度:O(n),其中 n 是数组 nums 的长度。
    • 空间复杂度:O(1)。

    10.6.3 方法二:迭代爬坡

    思路与算法
    在这里插入图片描述

    代码

    class Solution {
        public int findPeakElement(int[] nums) {
            int n = nums.length;
            int idx = (int) (Math.random() * n);
    
            while (!(compare(nums, idx - 1, idx) < 0 && compare(nums, idx, idx + 1) > 0)) {
                if (compare(nums, idx, idx + 1) < 0) {
                    idx += 1;
                } else {
                    idx -= 1;
                }
            }
            
            return idx;
        }
    
        // 辅助函数,输入下标 i,返回一个二元组 (0/1, nums[i])
        // 方便处理 nums[-1] 以及 nums[n] 的边界情况
        public int[] get(int[] nums, int idx) {		// 返回的数组第一个为0则越界,越界的值赋为0,为1没有越界
            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;
        }
    }
    
    • 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
    • 33
    • 34
    • 35
    • 36
    • 37

    复杂度分析

    • 时间复杂度:O(n),其中 n 是数组 nums 的长度。在最坏情况下,数组 nums 单调递增,并且我们随机到位置 0,这样就需要向右走到数组 nums 的最后一个位置。
    • 空间复杂度:O(1)。

    10.6.4 方法三:方法二的二分查找优化

    思路与算法
    在这里插入图片描述

    代码

    class Solution {
        public int findPeakElement(int[] nums) {
            int n = nums.length;
            int left = 0, right = n - 1, ans = -1;
            while (left <= right) {
                int mid = (left + right) / 2;
                if (compare(nums, mid - 1, mid) < 0 && compare(nums, mid, mid + 1) > 0) {
                    ans = mid;
                    break;
                }
                if (compare(nums, mid, mid + 1) < 0) {
                    left = mid + 1;
                } else {
                    right = mid - 1;
                }
            }
            return ans;
        }
    
        // 辅助函数,输入下标 i,返回一个二元组 (0/1, nums[i])
        // 方便处理 nums[-1] 以及 nums[n] 的边界情况
        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;
        }
    }
    
    • 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
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    复杂度分析

    • 时间复杂度:O(logn),其中 n 是数组 nums 的长度。
    • 空间复杂度:O(1)。

    10.6.5 my answer—找最大值

    class Solution {
        public int findPeakElement(int[] nums) {
            int i = 0;
            int ans = 0;
            int max = nums[i];
            for(i = 1;i<nums.length;i++){
                if(nums[i]>max){
                    max = nums[i];
                    ans = i;
                }
            }
            return ans;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    php+mysql公司日常办公OA系统
    【瑞吉外卖】day03:完善登录功能与新增员工
    我的高考往事
    480439-15-4,一种具有荧光单体的pH敏感性染料Fluorescein O-methacrylate
    Master JavaScript Coding
    工行里的数字员工是怎么来的?
    【EI会议征稿】第三届网络安全、人工智能与数字经济国际学术会议(CSAIDE 2024)
    MySQL索引
    第 21 章 InnoDB Cluster
    10-Mysql内核查询成本计算实战-05
  • 原文地址:https://blog.csdn.net/xiaoguanglin/article/details/126241964