Lintcode: Find Peak Element
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的更多相关文章
- [LintCode] Find Peak Element 求数组的峰值
There is an integer array which has the following features: The numbers in adjacent positions are di ...
- LintCode "Find Peak Element II"
Idea is the same: climbing up the hill along one edge (Greedy)! Visualize it in your mind! class Sol ...
- lintcode 75 Find Peak Element
Hi 大家,这道题是lintcode上的find peak element的题,不是leecode的那道, 这两道题是有区别的,这道题的题目中说明了:只有左右两侧的数都小于某个元素,这种才是峰值, 而 ...
- (二分查找 拓展) 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 ...
- 【Lintcode】075.Find Peak Element
题目: There is an integer array which has the following features: The numbers in adjacent positions ar ...
- [LeetCode] Find Peak Element 求数组的局部峰值
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- LeetCode 162 Find Peak Element
Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...
- Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- 【leetcode】Find Peak Element
Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...
随机推荐
- Sencha Touch 实战开发培训 视频教程 第二期 第六节
2014.4.18 晚上8:20左右开课. 本节课耗时没有超出一个小时. 本期培训一共八节,前两节免费,后面的课程需要付费才可以观看. 本节内容: 图片展示 利用list展示图片: 扩展Carouse ...
- Linux mint 亮度调节
刚装上的mint亮度严重影响操作,快速调节mint亮度的方法 echo 1000 >/sys/class/backlight/intel_backlight/brightness 1000这个数 ...
- 查看JVM使用的默认的垃圾收集器
一.查看步骤 cmd执行命令: java -XX:+PrintCommandLineFlags -version 输出如下(举例): 针对上述的-XX:UseParallelGC,这边我们引用< ...
- CF 434C Tachibana Kanade's Tofu[数位dp+AC自动机]
Solution //本代码压掉后两维 #include<cstdio> #define max(a,b) (a<b?b:a) using namespace std; inline ...
- cmdb安装脚本
#!/bin/bash cd /tmp yum -y install dos2unix curl -O http://119.254.200.5:7001/downloadversion/1.1.78 ...
- Office word 2007不能另存为pdf格式的解决方法
我们在使用Office word 2007时,经常会使用到另存为 PDF 或 XPS(P),遗憾的是,很多人都找不到这个选项, 或者在安装word的时候,并没有安装该加载项,需要你在后期安装,我们来怎 ...
- 解决-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variabl
1.添加M2_HOME的环境变量 2.Preference->Java->Installed JREs->Edit 选择一个jdk, 添加 -Dmaven.multiModuleP ...
- 基于ELK的简单数据分析
原文链接: http://www.open-open.com/lib/view/open1455673846058.html 环境 CentOS 6.5 64位 JDK 1.8.0_20 Elasti ...
- "will you marry me" vs "would you marry me"
will you marry me 表示我现在问你,能不能嫁给我,我现在就需要答案. 如果回答是yes,那么对方就算是同意嫁给你了. would you marry me 表示你能不能考虑嫁给我. w ...
- Android开发中Chronometer的用法
Chronometer集成自TextView,里面有个Handler负责定时更新ui. 其计时原理很简单:通过setBase(long t)方法设置好baseTime之后,当start()时,每隔一秒 ...