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)
  • 相关阅读:
    主键、外键
    SpringBoot定时任务Scheduled
    启动报DataSource错误
    SpringBoot整合aop
    元数据MetaData(五)
    普通结果集ResultSet和离线结果集RowSet(四)
    Statements、PreparedStatement及CallableStatement(三)
    JDBC数据库连接(二)
    JDBC简介(一)
    【Oracle】常用函数
  • 原文地址:https://www.cnblogs.com/h-hkai/p/9902410.html
Copyright © 2011-2022 走看看