博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Find a Peak Element
阅读量:4030 次
发布时间:2019-05-24

本文共 3372 字,大约阅读时间需要 11 分钟。

Find a peak element

Given an array of integers. Find a peak element in it. An array element is peak if it is NOT smaller than its neighbors. For corner elements, we need to consider only one neighbor. For example, for input array {5, 10, 20, 15}, 20 is the only peak element. For input array {10, 20, 15, 2, 23, 90, 67}, there are two peak elements: 20 and 90. Note that we need to return any one peak element.

Following corner cases give better idea about the problem.

1) If input array is sorted in strictly increasing order, the last element is always a peak element. For example, 50 is peak element in {10, 20, 30, 40, 50}.
2) If input array is sorted in strictly decreasing order, the first element is always a peak element. 100 is the peak element in {100, 80, 60, 50, 20}.
3) If all elements of input array are same, every element is a peak element.

It is clear from above examples that there is always a peak element in input array in any input array.

simple solution is to do a linear scan of array and as soon as we find a peak element, we return it. The worst case time complexity of this method would be O(n).

Can we find a peak element in worst time complexity better than O(n)?

We can use to find a peak in O(Logn) time. The idea is Binary Search based, we compare middle element with its neighbors. If middle element is greater than both of its neighbors, then we return it. If the middle element is smaller than the its left neighbor, then there is always a peak in left half (Why? take few examples). If the middle element is smaller than the its right neighbor, then there is always a peak in right half (due to same reason as left half). Following are C and Java implementations of this approach.

  • C/C++
  • Java
// A C++ program to find a peak element element using divide and conquer
#include <stdio.h>
 
// A binary search based function that returns index of a peak element
int
findPeakUtil(
int
arr[],
int
low,
int
high,
int
n)
{
    
// Find index of middle element
    
int
mid = low + (high - low)/2; 
/* (low + high)/2 */
 
    
// Compare middle element with its neighbours (if neighbours exist)
    
if
((mid == 0 || arr[mid-1] <= arr[mid]) &&
            
(mid == n-1 || arr[mid+1] <= arr[mid]))
        
return
mid;
 
    
// If middle element is not peak and its left neighbour is greater
    
// than it, then left half must have a peak element
    
else
if
(mid > 0 && arr[mid-1] > arr[mid])
        
return
findPeakUtil(arr, low, (mid -1), n);
 
    
// If middle element is not peak and its right neighbour is greater
    
// than it, then right half must have a peak element
    
else
return
findPeakUtil(arr, (mid + 1), high, n);
}
 
// A wrapper over recursive function findPeakUtil()
int
findPeak(
int
arr[],
int
n)
{
    
return
findPeakUtil(arr, 0, n-1, n);
}
 
/* Driver program to check above functions */
int
main()
{
    
int
arr[] = {1, 3, 20, 4, 1, 0};
    
int
n =
sizeof
(arr)/
sizeof
(arr[0]);
    
printf
(
"Index of a peak point is %d"
, findPeak(arr, n));
    
return
0;
}
Output:
Index of a peak point is 2

Time Complexity: O(Logn) where n is number of elements in input array.

Exercise:

Consider the following modified definition of peak element. An array element is peak if it is greater than its neighbors. Note that an array may not contain a peak element with this modified definition.

References:

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

转载地址:http://lihbi.baihongyu.com/

你可能感兴趣的文章
如何打开ipynb文件
查看>>
[Leetcode BY python ]190. Reverse Bits
查看>>
面试---刷牛客算法题
查看>>
Android下调用收发短信邮件等(转载)
查看>>
Android中电池信息(Battery information)的取得
查看>>
SVN客户端命令详解
查看>>
Android/Linux 内存监视
查看>>
Linux系统信息查看
查看>>
用find命令查找最近修改过的文件
查看>>
Android2.1消息应用(Messaging)源码学习笔记
查看>>
在android上运行native可执行程序
查看>>
Phone双模修改涉及文件列表
查看>>
android UI小知识点
查看>>
Android之TelephonyManager类的方法详解
查看>>
android raw读取超过1M文件的方法
查看>>
ubuntu下SVN服务器安装配置
查看>>
MPMoviePlayerViewController和MPMoviePlayerController的使用
查看>>
CocoaPods实践之制作篇
查看>>
[Mac]Mac 操作系统 常见技巧
查看>>
苹果Swift编程语言入门教程【中文版】
查看>>