[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;
    }

时间: 2024-10-02 21:49:55

[LeetCode]162.Find Peak Element的相关文章

[LeetCode] 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 fi

[LeetCode]230.Kth Smallest Element in a BST

题目 Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. Follow up: What if the BST is modified (insert/delete operations) often and you

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

[LeetCode]169.Majority Element

[题目] Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. [分析] 思路是计算数组前一半出现元素的频率,

[LeetCode]27.Remove Element

[题目] Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. [题意] 把数组中与给定值相同的元素删除,在原数组上修改,返回值是最终元素个数. [分析] 无 [代码]

leetcode 169 Majority Element 冰山查询

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. 思路: Find k different element

【LeetCode从零单排】No.169 Majority Element(hashmap用法)

题目 Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. Credits:Special thanks to

[LeetCode] Majority Element

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. 解题思路:摩尔投票法.这种投票法先将第一个数字假设为众数

LeetCode 27 Remove Element(移除元素)

翻译 给定一个数组和一个值,删除该值的所有实例,并返回新的长度. 元素的顺序可以被改变,也不关心最终的数组长度. 原文 Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new lengt