• Find Peak Element


    Problem

    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 returnits index.

    The array may contain multiple peaks, in that case return the index to any oneof 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 functionshould return the index number 2.

    Note:

    Your solution should be in logarithmic complexity.

    题解

    由时间复杂度的暗示可知应使用二分搜索。首先分析若使用传统的二分搜索,若A[mid] > A[mid - 1] && A[mid] < A[mid + 1],则找到一个peak为A[mid];若A[mid - 1] > A[mid],则A[mid]左侧必定存在一个peak,可用反证法证明:若左侧不存在peak,则A[mid]左侧元素必满足A[0] > A[1] > ... > A[mid -1] > A[mid],与已知A[0] < A[1]矛盾,证毕。同理可得若A[mid + 1] > A[mid],则A[mid]右侧必定存在一个peak。如此迭代即可得解。由于题中假设端点外侧的值均为负无穷大,即num[-1] < num[0] && num[n-1] > num[n], 那么问题来了,这样一来就不能确定峰值一定存在了,因为给定数组为单调序列的话就咩有峰值了,但是实际情况是——题中有负无穷的假设,也就是说在单调序列的情况下,峰值为数组首部或者尾部元素,谁大就是谁了。

    备注:如果本题是找 first/last peak,就不能用二分法了。

    C++

    class Solution {
    public:
        /**
         * @param A: An integers array.
         * @return: return any of peek positions.
         */
        int findPeak(vector<int> A) {
            if (A.size() == 0) return -1;
    
            int l = 0, r = A.size() - 1;
            while (l + 1 < r) {
                int mid = l + (r - l) / 2;
                if (A[mid] < A[mid - 1]) {
                    r = mid;
                } else if (A[mid] < A[mid + 1]) {
                    l = mid;
                } else {
                    return mid;
                }
            }
    
            int mid = A[l] > A[r] ? l : r;
            return mid;
        }
    };

    Java

    class Solution {
        /**
         * @param A: An integers array.
         * @return: return any of peek positions.
         */
        public int findPeak(int[] A) {
            if (A == null || A.length == 0) return -1;
    
            int lb = 0, ub = A.length - 1;
            while (lb + 1 < ub) {
                int mid = lb + (ub - lb) / 2;
                if (A[mid] < A[mid + 1]) {
                    lb = mid;
                } else if (A[mid] < A[mid - 1]){
                    ub = mid;
                } else {
                    // find a peak
                    return mid;
                }
            }
    
            // return a larger number
            return A[lb] > A[ub] ? lb : ub;
        }
    }

    源码分析

    典型的二分法模板应用,需要注意的是需要考虑单调序列的特殊情况。当然也可使用紧凑一点的实现如改写循环条件为l < r,这样就不用考虑单调序列了,见实现2.

    复杂度分析

    二分法,时间复杂度 O(logn).

    Java 

    public class Solution {
        public int findPeakElement(int[] nums) {
            if (nums == null || nums.length == 0) {
                return -1;
            }
    
            int start = 0, end = nums.length - 1, mid = end / 2;
            while (start < end) {
                if (nums[mid] < nums[mid + 1]) {
                    // 1 peak at least in the right side
                    start = mid + 1;
                } else {
                    // 1 peak at least in the left side
                    end = mid;
                }
                mid = start + (end - start) / 2;
            }
    
            return start;
        }
    }
  • 相关阅读:
    转载个人认为原创很不错的分析 :ISCSI网络存储
    使用centos-vault作为停止支持的CentOS的yum源
    AngularJS 第四课(选择框,HTML DOM,HTML 事件)
    AngularJS第三课(控制器(续),过滤器,服务)
    AngularJS第二课(指令,作用域,控制器)
    Angular.js学习笔记——第一课 简介
    简单的响应式表格布局
    在windows sever2012安装web服务器时出现“无法打开运行空间池。服务器管理器 WinRM 插件可能已损坏或丢失”
    HTML5中的全局属性
    href="#"和href="javascript:void(0)"的区别
  • 原文地址:https://www.cnblogs.com/lyc94620/p/12441934.html
Copyright © 2020-2023  润新知