Find Peak Element

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.

click to show spoilers.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

SOLUTION 1:

线性查找,时间O(N):

 public int findPeakElement1(int[] num) {
if (num == null) {
return 0;
} if (num.length == 1) {
return 0;
} for (int i = 0; i < num.length; i++) {
if (i == 0) {
if (num[i] > num[i + 1]) {
return i;
}
continue;
} if (i == num.length - 1) {
if (num[i] > num[i - 1]) {
return i;
}
continue;
} if (num[i] > num[i + 1] && num[i] > num[i - 1]) {
return i;
}
} return -1;
}

SOLUTION 2:

使用九章算法的二分法模板,可以达到O(logN)的时间复杂度。原理是:

当找到一个下坡,我们往左移动,当找到一个上坡,我们往右移动,这样我们就可以达到顶峰。

如果找到一个山谷,则向任意方向移动即可。

4

3          3     5

2    2    2

1          1

如上图所示,3,4都是可能的解。

最后循环break时,把l,r的值找一个大的即可。

 public int findPeakElement(int[] num) {
if (num == null) {
return 0;
} if (num.length == 1) {
return 0;
} int l = 0;
int r = num.length - 1; while (l < r - 1) {
int mid = l + (r - l) / 2;
if (num[mid] > num[mid + 1] && num[mid] > num[mid - 1]) {
return mid;
} if (num[mid] > num[mid - 1] && num[mid] < num[mid + 1]) {
// rising area. move right;
l = mid;
} else if (num[mid] < num[mid - 1] && num[mid] > num[mid + 1]) {
r = mid;
} else {
l = mid;
}
} return num[l] > num[r] ? l: r;
}

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/binarySearch/FindPeakElement.java

LeetCode: Find Peak Element 解题报告的更多相关文章

  1. 【LeetCode】162. Find Peak Element 解题报告(Python)

    [LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...

  2. 【原创】leetCodeOj --- Find Peak Element 解题报告

    题目地址: https://oj.leetcode.com/problems/find-peak-element/ 题目内容: A peak element is an element that is ...

  3. LeetCode 169 Majority Element 解题报告

    题目要求 Given an array of size n, find the majority element. The majority element is the element that a ...

  4. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  5. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  6. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  7. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  8. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  9. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

随机推荐

  1. 实现分布式服务注册及简易的netty聊天

    现在很多地方都会用到zookeeper, 用到它的地方就是为了实现分布式.用到的场景就是服务注册,比如一个集群服务器,需要知道哪些服务器在线,哪些服务器不在线. ZK有一个功能,就是创建临时节点,当机 ...

  2. 移动端滑屏全应用【一】cssHandler操作基础动画函数封装

    前言: 大家都知道,在移动端进行操作结点移动时,我们都会使用操作transform来代替top等用以提高性能,必要的时候还可开启3d加速.我们都会使用getComputedStyle来获取结点的最终样 ...

  3. 几个文本处理工具的简单使用(wc,cut,sort,uniq,diff和patch)

    wc wc命令用于报告文本文件的一些统计计数,例如行数.单词数.字节数等. 语法如下. wc [OPTION]... [FILE]... wc [OPTION]... --files0-from=F ...

  4. linux VIM 下的语法高亮及自动缩进

    显示行号 set number 自动缩进有两个选项 set autoindent set cindent autoindent 就是自动缩进的意思,当你在输入状态用回车键插入一个新行,或者在 norm ...

  5. jQuery File Upload 判断图片尺寸,限定图片宽高的办法

    1.必须熟读jQuery File Upload 文档,在add方法中进行判断,如果不符合条件,就用 data.abort()方法取消上传动作. $("file").fileupl ...

  6. 【洛谷】3960:列队【Splay】

    P3960 列队 题目描述 Sylvia 是一个热爱学习的女孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Sylvia 所在的方阵中有n×m名学生,方阵的行数为  ...

  7. C++学习笔记43:STL

    STL简介(standard Template Library) STL的基本组件:容器(container),迭代器(iterator),函数对象(function object) 算法(algor ...

  8. git 修改文件内容

    在安装Git和创建版本库的时候,我们已经成功地添加并提交了一个readme.txt文件,现在,是时候继续工作了,于是,我们继续修改readme.txt文件,改成如下内容  [root@node1 gi ...

  9. U3D面试题六

    U3D面试题大全   1.Unity中碰撞器(Collider)和触发器(Trigger)的区别? 两方都必须要有碰撞体,且一方要有刚体,但在发生碰撞的条件下,Collide碰撞器会生产 碰撞反弹的物 ...

  10. 独热编码和dummy编码的作用

    参考这篇文章: https://www.cnblogs.com/lianyingteng/p/7792693.html 总结:我们使用one-hot编码时,通常我们的模型不加bias项 或者 加上bi ...