Question

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.

Note:

Your solution should be in logarithmic complexity.

Solution

The assumption is a very good hint. It assures that there must be a peak in input array.

We can solve this problem by Binary Search.

Four situations to consider:

1. nums[mid] > nums[mid - 1] && nums[mid] > nums[mid + 1]

=> mid is a peak

2. nums[mid] > nums[mid - 1] && nums[mid] < nums[mid + 1]

=> There must exists a peak in right side

3. nums[mid] < nums[mid - 1] && nums[mid] > nums[mid + 1]

=> There must exists a peak in left side

4. nums[mid] < nums[mid - 1] && nums[mid] < nums[mid + 1]

=> Either in right or left side, there must exists a peak.

 public class Solution {
public int findPeakElement(int[] nums) {
// Binary Search to find peak
int start = 0, end = nums.length - 1, mid = 0, prev = 0, next = 0;
while (start + 1 < end) {
mid = (end - start) / 2 + start;
prev = mid - 1;
next = mid + 1;
if (nums[mid] > nums[prev] && nums[mid] > nums[next])
return mid;
if (nums[mid] > nums[prev] && nums[mid] < nums[next]) {
start = mid;
continue;
}
if (nums[mid] < nums[prev] && nums[mid] > nums[next]) {
end = mid;
continue;
}
start = mid;
}
if (nums[start] > nums[end])
return start;
return end;
}
}

Find Peak Element 解答的更多相关文章

  1. [LeetCode] Find Peak Element 求数组的局部峰值

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  2. LeetCode 162 Find Peak Element

    Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...

  3. Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  4. [LintCode] Find Peak Element 求数组的峰值

    There is an integer array which has the following features: The numbers in adjacent positions are di ...

  5. lintcode 75 Find Peak Element

    Hi 大家,这道题是lintcode上的find peak element的题,不是leecode的那道, 这两道题是有区别的,这道题的题目中说明了:只有左右两侧的数都小于某个元素,这种才是峰值, 而 ...

  6. 【leetcode】Find Peak Element

    Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...

  7. Java for LeetCode 162 Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  8. LeetCode Find Peak Element

    原题链接在这里:https://leetcode.com/problems/find-peak-element/ 题目: A peak element is an element that is gr ...

  9. 162. Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

随机推荐

  1. global.asax?app.config?webconfig??

    一.Global.asax 1.global.asax是什么? 一个文本文件,至于它包含写什么内容?顾名思义,global 肯定是掌管一个应用程序(application)的全局性的东西,例如应用程序 ...

  2. python3 urllib.request.urlopen() 地址打开错误

    错误内容:UnicodeEncodeError: 'ascii' codec can't encode characters in position 28-29: ordinal not in ran ...

  3. Struts2简单例子

    Struts实现注册功能 ControlFilter.java package com.jikexueyuan.filter; import java.io.IOException; import j ...

  4. json对象与字符串的相互转换,数组和字符串的转换

    1.json对象转换为字符串 JSON.stringify(value [, replacer] [, space])  var student = new Object(); student.id ...

  5. 看了一本书,说可以利用Hierarchy Viewer优化布局

    看了一本书,说可以利用Hierarchy Viewer优化布局,今以志之. 参考:http://www.cnblogs.com/Rocky_/archive/2011/11/04/2236243.ht ...

  6. 使用PLSql连接Oracle时报错ORA-12541: TNS: 无监听程序

    非常多时候为了优化我们的启动项把oracle的服务禁止了.但是重新启动启动之后使用PLSQL登陆oracle时会出现无监听程序,这说明我们有一些服务没有启动.我们先查看一下oracle的服务是否启动, ...

  7. Dynamics CRM 开发模板使用手册(插件开发)

    CRM开发手册 本手册介绍在Visual Studio 2015 + Dynamics CRM Developer Extensions模板开发环境下,插件和JS脚本的开发.部署与调试过程. 手册中提 ...

  8. VS2013以管理员身份使用

    Win8系统: 1.将C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe改为以管理员身份运行. 2.将 ...

  9. FineUI上传文件应用(三)

    一.文件上传控件 <x:FileUpload runat="server" ID="file" EmptyText="请选择文件" L ...

  10. 高级子查询【weber出品必属精品】

    多列子查询 where条件中出现多列与子查询进行比较 多列子查询分为:成对比较和非成对比较 成对比较: SQL> select ename,sal,job from emp where (dep ...