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. ios-toolchain-based-on-clang-for-linux

    https://github.com/tpoechtrager/cctools-port.git https://www.embtoolkit.org

  2. STM8L外部中断 为何 死循环 寄存器操作

    STM8L 系列单片机是 ST公司推出的低功耗单片机,与STM8S系列相比功耗降低了很多,但内部结构也删减了很多,使用时一定要仔细阅读手册.  这是第一次使用STM8,实现功能不是很复杂就没想研究库函 ...

  3. 算法学习之快速排序的C语言实现

    近几天在学习简单算法,今天看了一个快速排序和堆排序,堆排序还没搞懂,还是先把快速排序搞清楚吧 教程网上一艘一大堆,这里选择一个讲的比较通俗的的一个吧: http://blog.csdn.net/mor ...

  4. mysql多列索引优化

    “把Where条件里面的列都建上索引”,这种说法其实是非常错误的! 这样一个查询,假设actor_id与film_id都单独建立索引 SELECT film_id , actor_id FROM sa ...

  5. SSH客户端提示 用户密钥未在远程主机上注册

    今天在一台使用已久的内网服务器上面帮一位新同事添加账户,添加完成之后就把账号交付于他,过了10分钟他告诉我说无法登陆,觉得很诧异 这么轻车熟路的 这么会 登陆不上去了,自己也用Xshell 登陆了一下 ...

  6. Python面向对象之方法

    普通方法要执行类里面的方法是通过对象触发的 触发的时候把自己赋值给self 类方法 vim day7-7.py #!/usr/bin/python # -*- coding:utf-8 -*- cla ...

  7. HiveQL之Database相关操作

    1.Create Database(创建数据库语法) CREATE (DATABASE|SCHEMA) [IF NOT EXISTS] database_name [COMMENT database_ ...

  8. Oracle HA 之 测试RAC的功能

    作用:在oracle数据库instance级别的冗余,其中只要有一个instance可用即可保证可用性,但是不能保准数据级别的错误. 数据库文件需要放置在共享存储上,理论上一个实例对应一个数据库,实例 ...

  9. linux:进程概念

    Linux进程概念 一.实验介绍1.1 实验内容Linux 中也难免遇到某个程序无响应的情况,可以通过一些命令来帮助我们让系统能够更流畅的运行. 而在此之前,我们需要对进程的基础知识有一定的了解,才能 ...

  10. Nginx Upstream timed out (110: Connection timed out)

    Nginx Upstream timed out (110: Connection timed out) – 运维生存时间 http://www.ttlsa.com/nginx/nginx-upstr ...