There is an integer array which has the following features:

    * The numbers in adjacent positions are different.

    * A[0] < A[1] && A[A.length - 2] > A[A.length - 1].

We define a position P is a peek if A[P] > A[P-1] && A[P] > A[P+1].

Find a peak in this array. Return the index of the peak.

Note
The array may contains multiple peeks, find any of them. Example
[1, 2, 1, 3, 4, 5, 7, 6] return index 1 (which is number 2) or 6 (which is number 7) Challenge
Time complexity O(logN)

跟Leetcode Find Peak Element一样

有一些考虑:因为梯度下降法是要比较m、m+1、m-1三个index大小,因此为保证不outofbound,令l = 1, r = A.length-2; 这样也可以maintain一个性质:l、r始终在peak element可能的区域内

 class Solution {
/**
* @param A: An integers array.
* @return: return any of peek positions.
*/
public int findPeak(int[] A) {
if (A==null || A.length<3) return -1;
int l = 1;
int r = A.length - 2;
while (l <= r) {
int m = (l + r) / 2;
if (A[m]>A[m+1] && A[m]>A[m-1]) return m;
else if (A[m]<A[m+1] && A[m]>A[m-1]) {
l = m + 1;
}
else {
r = m - 1;
}
}
return -2;
}
}

Lintcode: Find Peak Element的更多相关文章

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

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

  2. LintCode "Find Peak Element II"

    Idea is the same: climbing up the hill along one edge (Greedy)! Visualize it in your mind! class Sol ...

  3. lintcode 75 Find Peak Element

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

  4. (二分查找 拓展) leetcode 162. Find Peak Element && lintcode 75. Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...

  5. 【Lintcode】075.Find Peak Element

    题目: There is an integer array which has the following features: The numbers in adjacent positions ar ...

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

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

  7. LeetCode 162 Find Peak Element

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

  8. Find Peak Element

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

  9. 【leetcode】Find Peak Element

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

随机推荐

  1. JavaScript中Array

    一,针对于数组 const arr = ['a','b','c','d']; Array.indexOf  将“返回第一次出现给定元素的索引”; console.log(arr.indexOf('b' ...

  2. syslinux 和 grub

    syslinux是一个功能强大的引导加载程序,而且兼容各种介质.它的目的是简化首次安装Linux的时间,并建立修护或其它特殊用途的启动盘.它的安装很简单,一旦安装syslinux好之后,sysLinu ...

  3. explain 和 desc 详解

    MySQL性能分析 1.使用explain语句去查看分析结果 如explain select * from test1 where id=1;会出现:id  selecttype  table  ty ...

  4. 用js内置对象XMLHttpRequest 来用ajax

    步骤: /* 用XMLHTTPRequest来进行ajax异步数据交交互*/ 主要有几个步骤: //1.创建XMLHTTPRequest对象 //最复杂的一步 if (window.XMLHttpRe ...

  5. 解决IE6下透明图片有背景的问题

    描述,透底的图片,在其他浏览器中效果: 在IE6中的效果:  1.改图片 IMG 换用gif格式的图片,但是这样的话图片效果不好: 想让IE6用gif的图片,其他浏览器用png图片,但是只有IE有这样 ...

  6. spring配置多视图解析器

    最近做一个小项目(移动端),自己搭了个简单的SSM框架(spring + spring MVC + Mybitis),展示层本来选用的是jsp,各方便都已经搭建好,结果发现有些页面需要用到H5的一些功 ...

  7. scrapy-redis的使用与解析

      scrapy-redis是一个基于redis的scrapy组件,通过它可以快速实现简单分布式爬虫程序,该组件本质上提供了三大功能: scheduler - 调度器 dupefilter - URL ...

  8. python类中的self参数和cls参数

    1. self表示一个类的实例对象本身.如果用了staticmethod就无视这个self了,就将这个方法当成一个普通的函数使用了. 2. cls表是这个类本身. # 代码为证 class A(obj ...

  9. hdu3974 Assign the task【线段树】

    There is a company that has N employees(numbered from 1 to N),every employee in the company has a im ...

  10. 任何时候心中都要有WBS的模版树---产品模块级项目