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. Excel中countif函数的使用方法

    1.countif函数的含义 在指定区域中按指定条件对单元格进行计数(单条件计数) 建议和countifs函数结合起来学习,可以观看小编的经验Excel中countifs函数的使用方法. END 2. ...

  2. iframe内点击a标签禁止滚动到顶部

    在iframe内加载的表中,操作下的按钮用a标签布局,但是会出现一个非常不好的体验,就是当页面有滚动条的时候,点击a标签,列表会自动滚动到顶部. 首先看我的a标签: <a href=" ...

  3. [NOI2005]月下柠檬树[计算几何(simpson)]

    1502: [NOI2005]月下柠檬树 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1169  Solved: 626[Submit][Status] ...

  4. MySQL外键和高级查询(连接查询、联合查询、子查询、去重查询)

    MySQL的外键 什么是外键,很简单保持数据一致性的一个约束键.如果你有两张表,第一张是学生表,第二张表是一个成绩表,我们来看看保持数据一致性,其实在Django等框架的模型中中也能做关联获取对象. ...

  5. Google浏览器清除缓存快捷键

    1.CTRL+SHIFT+DEL:直接进入“清除浏览数据”页面,包括清除浏览历史记录.清空缓存.删除Cookie等. 2.chrome浏览器F12中 ctrl+p 可以定位文件

  6. 无线路由器无线AP模式的配置

    环境介绍>>>>>>>>>>>>>>>>>>>交换机类型:三层交换机无线路由器品牌:T ...

  7. 【BZOJ4445】[Scoi2015]小凸想跑步 半平面交

    [BZOJ4445][Scoi2015]小凸想跑步 Description 小凸晚上喜欢到操场跑步,今天他跑完两圈之后,他玩起了这样一个游戏. 操场是个凸n边形,N个顶点按照逆时针从0-n-l编号.现 ...

  8. 为什么用VUE,而不用Jquery了?

    在没有任何前端框架之前,我们写代码,只能用原生的JS,进行数据的处理,DOM的操作,譬如对一个id 为txtName 的文本框进行赋值,我们是这样的 document.getElementById(' ...

  9. JAVA补充-接口

    1.接口详解 package com.neusoft.interfaced; /** * 接口: * 语法:interface 接口名{ * public static final 变量1=值1: * ...

  10. intel笔记本cpu型号后缀详解(M,U,QM,MQ,HQ,XM)

    M:笔记本专用CPU,一般为双核,M前面一位数字是0,意味着是标准电压处理器,如果是7,则是低电压处理器. U:笔记本专用低电压CPU,一般为双核,U前面一位数字为8,则是28W功耗的低压处理器(标准 ...