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. mybatis 调用mysql存储过程 带输出输入参数

    http://lohasle.iteye.com/blog/1669879 存储过程都是一样的,只是根据自己的喜好,可以用MAP或者JAVABEAN传递参数. -- ----------------- ...

  2. AVR32开发环境搭建

    下面是搭建AVR32开发环境的过程记录: 1.AVR32的编译环境下载  (到这里下载  as5installer-stable-5.1.208-full.exe) 如果你在安装的过程中碰到如下问题: ...

  3. GDB实用的调试工具

    GDB它是GNU Debuger缩写,它是GNU发表了unix通过应用程序调试工具. 它被广泛应用于在各种各种生产内部应用.GDB和所有的调试工具经常使用相同的,其主要特点是:监视变量的值.设置断点及 ...

  4. Android 自定义UI--电池

    首先看一下效果图, 下面看代码: /** * */ package com.example.batterydemo; import android.content.Context; import an ...

  5. spark 高级算子

      mapPartitionsWithIndex val func = (index: Int, iter: Iterator[(Int)]) => {   iter.toList.map(x  ...

  6. 批量SSH操作工具---OmniTTY安装

    安装rote # pwd /tmp/rote-0.2.8 # ./configure # make # make install ...... mkdir -p /usr/local/include/ ...

  7. js字母大小写转换

    function a(){ document.getElementById("test").value = document.getElementById("test&q ...

  8. C#编写Windows服务程序图文教程(转载)

    Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...

  9. (一)Activity参数传递

    1.主Activity,用于启动另一个Activity()public class MainActivity extends Activity { @Override protected void o ...

  10. mac中遇到的mysql编码问题

    由于项目有需要支持表情包输入数据库,自己做了一下技术测试,修改了my.cnf的权限为777.结果就操蛋了.编码错误.... 直到我无意地输入mysql -h,提示我/etc/my.cnf被忽略的一段话 ...