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)
  • 相关阅读:
    设计模式之适配器模式(Decorator)
    可复用面向对象软件的基础
    dpkg命令的用法
    UML类图几种关系的总结
    Java多线程并发编程之原子变量与非阻塞同步机制
    Java内部类总结
    多线程并发编程之构建自定义同步工具
    多线程并发编程之显示锁ReentrantLock和读写锁
    多线程并发编程之变量
    汇编语言学习系列 冒泡排序实现
  • 原文地址:https://www.cnblogs.com/h-hkai/p/9902410.html
Copyright © 2011-2022 走看看