[LeetCode]162.Find Peak Element

简介:

【题目】

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.

【分析一】

我们首先想到的就是时间复杂度为O(n)的顺序遍历,对每一个元素,与它相邻的元素比较。

这样也可以AC。

Your solution should be in logarithmic complexity.

可是题目要求是时间复杂度是对数级别的。

【代码一】

/*********************************
*   日期:2015-01-31
*   作者:SJF0115
*   题目: 162.Find Peak Element
*   网址:https://oj.leetcode.com/problems/find-peak-element/
*   结果:AC
*   来源:LeetCode
*   博客:
**********************************/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Solution {
public:
    int findPeakElement(const vector<int> &num) {
        int size = num.size();
        if(size == 1){
            return 0;
        }//if
        // 判断第一个元素和最后一个元素
        if(size > 1){
            if(num[0] > num[1]){
                return 0;
            }//if
            if(num[size-1] > num[size-2]){
                return size-1;
            }//if
        }//if
        for(int i = 1;i < size-1;++i){
            if(num[i] > num[i-1] && num[i] > num[i+1]){
                return i;
            }//if
        }//for
    }
};

int main(){
    Solution solution;
    // vector<int> num = {1,2,3,1};
    //vector<int> num = {4,3,3,1};
    vector<int> num = {1,2,3,4};
    int result = solution.findPeakElement(num);
    // 输出
    cout<<result<<endl;
    return 0;
}


【分析二】

根据给出的条件: num[i] != num[i+1], 相邻两个元素不相等。运用二分查找原理。

(1)如果 num[i-1] < num[i] > num[i+1], 则num[i] 就是 peak

(2)如果 num[i-1] < num[i] < num[i+1], 则 num[i+1...n-1] 必定包含一个 peak

(3)如果 num[i-1] > num[i] > num[i+1], 则num[0...i-1] 必定包含一个 peak

(4)如果 num[i-1] > num[i] < num[i+1], 则 两边都有一个 peak

继续优化一下,通过上面仔细观察一下:

(1)如果 num[i-1] < num[i] > num[i+1], 则num[i] 就是 peak

(2)如果 num[i-1] < num[i] , 则 num[i+1...n-1] 必定包含一个 peak,left指向mid+1

(3)如果 num[i-1] > num[i] , 则num[0...i-1] 必定包含一个 peak,right指向mid-1

【代码二】

    /*------------------------------------
    *   日期:2015-01-31
    *   作者:SJF0115
    *   题目: 162.Find Peak Element
    *   网址:https://oj.leetcode.com/problems/find-peak-element/
    *   结果:AC
    *   来源:LeetCode
    *   博客:
    ---------------------------------------*/
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;

    class Solution {
    public:
        int findPeakElement(const vector<int> &num) {
            int size = num.size();
            // only one element
            if (size == 1) {
                return 0;
            }//if
            int left = 0, right = size - 1;
            int mid;
            // left right 距离为1 退出
            while (left < right - 1) {
                mid = left + (right - left) / 2;
                // If num[i-1] < num[i] > num[i+1],then num[i] is peak
                if (num[mid] > num[mid - 1] && num[mid] > num[mid + 1]) {
                    return mid;
                }//if
                // If num[i-1] < num[i],then num[i+1...n-1] must contains a peak
                if (num[mid - 1] < num[mid]) {
                    left = mid + 1;
                }//if
                // If num[i-1] > num[i], then num[0...i-1] must contains a peak
                else {
                    right = mid - 1;
                }//else
            }//while
            return num[left] > num[right] ? left : right;
        }
    };

    int main(){
        Solution solution;
        vector<int> num = {1,2,3,1};
        //vector<int> num = {4,3,3,1};
        //vector<int> num = {2,3,1,4,2,1};
        int result = solution.findPeakElement(num);
        // 输出
        cout<<result<<endl;
        return 0;
    }

【分析三】

递归版

【代码三】

    /*------------------------------------
    *   日期:2015-01-31
    *   作者:SJF0115
    *   题目: 162.Find Peak Element
    *   网址:https://oj.leetcode.com/problems/find-peak-element/
    *   结果:AC
    *   来源:LeetCode
    *   博客:
    ---------------------------------------*/
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;

    class Solution {
    public:
        int findPeakElement(const vector<int> &num) {
            return helper(num,0,num.size()-1);
        }
    private:
        int helper(const vector<int> &num,int left,int right){
            // only one element
            if(left == right){
                return left;
            }//if
            //  neighbor
            if(left == right - 1){
                return num[left] > num[right] ? left : right;
            }//if
            int mid = left + (right - left) / 2;
            // If num[i-1] < num[i] > num[i+1], then num[i] is peak
            if(num[mid] > num[mid-1] && num[mid] > num[mid+1]){
                return mid;
            }//if
            // If num[i-1] > num[i], then num[0...i-1] must contains a peak
            if(num[mid] < num[mid - 1]){
                return helper(num,left,mid - 1);
            }//if
            // If num[i-1] < num[i],then num[i+1...n-1] must contains a peak
            else{
                return helper(num,mid + 1,right);
            }//else
        }
    };

    int main(){
        Solution solution;
        //vector<int> num = {1,2,3,1};
        //vector<int> num = {4,3,3,1};
        vector<int> num = {2,3,1,4,2,1};
        int result = solution.findPeakElement(num);
        // 输出
        cout<<result<<endl;
        return 0;
    }


目录
相关文章
|
6月前
Leetcode 230. Kth Smallest Element in a BST
先来看下二叉搜索树的性质,对于任意一个非叶子节点,它的左子树中所有的值必定小于这个节点的val,右子树中所有的值必定大于或等于当前节点的val。 这条性质就保证了如果我们对二叉搜索树做中序遍历,中序遍历的结果肯定是有序的。对于此题而言,我们只需要拿到中序遍历结果,然后返回第k个即可,时间复杂度是O(n)。
46 1
|
算法 Python
LeetCode 169. 多数元素 Majority Element
LeetCode 169. 多数元素 Majority Element
|
Python
LeetCode 378. Kth S Element in a Sorted Matrix
给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素。 请注意,它是排序后的第k小元素,而不是第k个元素。
79 0
LeetCode 378. Kth S Element in a Sorted Matrix
|
算法
LeetCode 229. Majority Element II
给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素。 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1)。
61 0
LeetCode 229. Majority Element II
|
索引
LeetCode 162. Find Peak Element
给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元素并返回其索引。 数组可能包含多个峰值,在这种情况下,返回任何一个峰值所在位置即可。
75 0
LeetCode 162. Find Peak Element
|
Java C++
LeetCode之Remove Element
LeetCode之Remove Element
87 0
LeetCode之Next Greater Element I
LeetCode之Next Greater Element I
60 0
|
算法 Java
LeetCode 229 Majority Element II(主要元素II)(Array)(Boyer–Moore majority vote algorithm)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/52356817 原文 给定一个长度为n的整型数组,找出所有出现超过 ⌊ n/3 ⌋ 次的元素。
1006 0
|
容器
LeetCode 169 Majority Element(主要元素)(vector、map)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50504698 翻译 给定一个长度为n的数组,找出主要的元素。
783 0