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)
  • 相关阅读:
    day50_BOS项目_02
    day51_BOS项目_03
    Criteria 和 DetachedCriteria 的区别
    ecplise中如何关闭Tomcat的自动重启/加载
    Tomcat的Start可以启动起来,但是Debug启动突然启动不起来,一直停在 Class<T>.getDeclaredConstructors0(boolean) line: not available [native method] 的解决办法
    windows下读取Linux分区软件
    15 条实用 Linux/Unix 磁带管理命令
    Nginx安装与配置文件解析
    专注docker安全:Security Scanning
    清除linux系统的多余引导
  • 原文地址:https://www.cnblogs.com/h-hkai/p/9902410.html
Copyright © 2011-2022 走看看