zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    CodeForces 1294B Collecting Packages(排序+贪心)
    计算系数(组合数)
    垒骰子(矩阵快速幂)
    蒜头君倒水(矩阵快速幂)
    Buy Tickets POJ
    Billboard HDU
    树状数组求逆序对 附HDU1394
    codeforces 1304D Shortest and Longest LIS
    codeforces 1301C Ayoub's function
    Codeforces 1301B Motarack's Birthday(二分)
  • 原文地址:https://www.cnblogs.com/h-hkai/p/9902410.html
Copyright © 2011-2022 走看看