Find Peak Element

A peak element is an element that is greater than its neighbors.

Given an input array where num[i] ≠ num[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 num[-1] = num[n] = -∞.

For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2.

click to show spoilers.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

SOLUTION 1:

线性查找,时间O(N):

  1. public int findPeakElement1(int[] num) {
  2. if (num == null) {
  3. return 0;
  4. }
  5.  
  6. if (num.length == 1) {
  7. return 0;
  8. }
  9.  
  10. for (int i = 0; i < num.length; i++) {
  11. if (i == 0) {
  12. if (num[i] > num[i + 1]) {
  13. return i;
  14. }
  15. continue;
  16. }
  17.  
  18. if (i == num.length - 1) {
  19. if (num[i] > num[i - 1]) {
  20. return i;
  21. }
  22. continue;
  23. }
  24.  
  25. if (num[i] > num[i + 1] && num[i] > num[i - 1]) {
  26. return i;
  27. }
  28. }
  29.  
  30. return -1;
  31. }

SOLUTION 2:

使用九章算法的二分法模板,可以达到O(logN)的时间复杂度。原理是:

当找到一个下坡,我们往左移动,当找到一个上坡,我们往右移动,这样我们就可以达到顶峰。

如果找到一个山谷,则向任意方向移动即可。

4

3          3     5

2    2    2

1          1

如上图所示,3,4都是可能的解。

最后循环break时,把l,r的值找一个大的即可。

  1. public int findPeakElement(int[] num) {
  2. if (num == null) {
  3. return 0;
  4. }
  5.  
  6. if (num.length == 1) {
  7. return 0;
  8. }
  9.  
  10. int l = 0;
  11. int r = num.length - 1;
  12.  
  13. while (l < r - 1) {
  14. int mid = l + (r - l) / 2;
  15. if (num[mid] > num[mid + 1] && num[mid] > num[mid - 1]) {
  16. return mid;
  17. }
  18.  
  19. if (num[mid] > num[mid - 1] && num[mid] < num[mid + 1]) {
  20. // rising area. move right;
  21. l = mid;
  22. } else if (num[mid] < num[mid - 1] && num[mid] > num[mid + 1]) {
  23. r = mid;
  24. } else {
  25. l = mid;
  26. }
  27. }
  28.  
  29. return num[l] > num[r] ? l: r;
  30. }

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/binarySearch/FindPeakElement.java

LeetCode: Find Peak Element 解题报告的更多相关文章

  1. 【LeetCode】162. Find Peak Element 解题报告(Python)

    [LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...

  2. 【原创】leetCodeOj --- Find Peak Element 解题报告

    题目地址: https://oj.leetcode.com/problems/find-peak-element/ 题目内容: A peak element is an element that is ...

  3. LeetCode 169 Majority Element 解题报告

    题目要求 Given an array of size n, find the majority element. The majority element is the element that a ...

  4. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  5. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  6. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  7. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  8. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  9. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

随机推荐

  1. poj 1797 最大最小路段【dijkstra】 (经典)

    <题目链接> 题目大意: Hugo Heavy要从城市1到城市N运送货物,有M条道路,每条道路都有它的最大载重量,问从城市1到城市N运送最多的重量是多少. 解题分析: 感觉这道题用dijk ...

  2. MongoDB——权限管理

    MongoDB--权限管理 MongoDB默认是没有权限验证的,但生产环境中,没有权限控制是很不安全的. 我们先不详谈太多概念,直接动手创建两个典型的账号: 超级管理员,类似sql server的sa ...

  3. 2016/2/26Android实习笔记(Android签名和aapt)

    1. 我们平时用eclipse或Android Studio开发得到的android应用程序,其实已经添加有默认的debug签名了. Android系统要求所有的程序经过数字签名才能安装,如果没有可用 ...

  4. Android Studio 使用过程遇到的坑

    最近在尝试Android Studio打Jar的包,然而事实并不是想象的那么简单,so,写多个坑的解决,以备不时之需. 1.Error:Execution failed for task ':app: ...

  5. MYSQL时间类别总结: TIMESTAMP、DATETIME、DATE、TIME、YEAR

    总结背景: 对于MYSQL数据库日期类型或多有了解, 但并很清晰其中一些规则. 基本都是面向浏览器编码, 这实质上也是一种方式.  但期间遇到两个问题: 时常遇到建表中出现多个datetime或者ti ...

  6. Linux中ls -l(ll)返回结果中的文件访问权限-rw-r--rw-

    linux文件访问权限(像rw-r--rw-是什么意思)   Linux的文件访问权限分为 读.写.执行三种 r:可读(4) w:可写(2)对目录来说则可新建文件 x:可执行(1)对目录来说则可进入该 ...

  7. Standard Series Values in a Decade for Resistances and Capacitances E24 E48 E96

    E3     50% tolerance (no longer used)E6     20% tolerance (now seldom used)E12   10% toleranceE24    ...

  8. delphi获取文件的创建/修改时间、按时间删除指定文件下的文件

    uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,   Dialogs, StdCtrl ...

  9. 设备树中ranges属性分析(1)

    作者 彭东林 pengdonglin137@163.com   软件环境 Linux-4.10.17 Qemu+vexpress   概述 在设备树中有时会看到ranges属性,这个ranges属性可 ...

  10. 写一个针对IQueryable<T>的扩展方法支持动态排序

    所谓的动态排序是指支持任意字段.任意升序降序的排序.我们希望在客户端按如下格式写: localhost:8000/api/items?sort=titlelocalhost:8000/api/item ...