• 162. Find Peak Element


    A peak element is an element that is greater than its neighbors.

    Given an input array nums, where nums[i] ≠ nums[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 nums[-1] = nums[n] = -∞.

    Example 1:

    Input: nums = [1,2,3,1]
    Output: 2
    Explanation: 3 is a peak element and your function should return the index number 2.

    Example 2:

    Input: nums = [1,2,1,3,5,6,4]
    Output: 1 or 5 
    Explanation: Your function can return either index number 1 where the peak element is 2, 
                 or index number 5 where the peak element is 6.
    

    Note:

    Your solution should be in logarithmic complexity.

    Approach #1:

    class Solution {
    public:
        int findPeakElement(vector<int>& nums) {
            int len = nums.size();
            if (len == 1) return 0;
            int start = 1, end = len-2;
            while (nums[0] == nums[start] && start < len) start++;
            if (nums[0] > nums[start]) return 0;
            while (nums[len-1] == nums[end] && end >= 0) end--;
            if (nums[len-1] > nums[end]) return len-1;
            for (int i = start; i <= end; ++i) {
                if (nums[i] > nums[i-1] && nums[i] > nums[i+1]) return i;
            }
        }
    };
    
    Runtime: 8 ms, faster than 14.40% of C++ online submissions for Find Peak Element.

    Approach #2: Linear Scan

    class Solution {
    public:
        int findPeakElement(vector<int>& nums) {
            int len = nums.size();
            for (int i = 0; i < len-1; ++i) {
                if (nums[i] > nums[i+1])
                    return i;
            }
            return len-1;
        }
    };
    

    Runtime: 4 ms, faster than 99.33% of C++ online submissions for Find Peak Element.

    class Solution {
    public:
        int findPeakElement(vector<int>& nums) {
            int len = nums.size();
            return search(nums, 0, len-1);
        }
        
        int search(vector<int> nums, int l, int r) {
            if (l == r) return l;
            int m = l + (r - l) / 2;
            if (nums[m] > nums[m+1]) return search(nums, l, m);
            return search(nums, m+1, r);
        }
    };
    
    Runtime: 4 ms, faster than 99.33% of C++ online submissions for Find Peak Element.
    class Solution {
    public:
        int findPeakElement(vector<int>& nums) {
            int len = nums.size();
            int l = 0, r = len - 1;
            while (l < r) {
                int m = l + (r - l) / 2;
                if (nums[m] > nums[m+1])
                    r = m;
                else
                    l = m + 1;
            }
            return l;
        }
        
    };
    
    Runtime: 4 ms, faster than 99.33% of C++ online submissions for Find Peak Element.

    when i modify the condition with:

            while (l <= r) {
                int m = l + (r - l) / 2;
                if (nums[m] > nums[m+1])
                    r = m - 1;
                else
                    l = m + 1;
            }
    

    it will report error.

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    绿色版Notepad++ 加右键带图标菜单
    C#中string和StringBuilder的区别
    C#中string和String的区别
    C#中is和as的区别
    C#中抽象类(abstract)和接口(interface)的相同点与区别
    c++串口通信实例
    vs2017常用快捷键
    Qt编译opencv找不到头文件
    Qt常用快捷键
    二维数组和指针
  • 原文地址:https://www.cnblogs.com/h-hkai/p/9902410.html
Copyright © 2020-2023  润新知