Find Peak Element 解答
Question
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.
Your solution should be in logarithmic complexity.
Solution
The assumption is a very good hint. It assures that there must be a peak in input array.
We can solve this problem by Binary Search.
Four situations to consider:
1. nums[mid] > nums[mid - 1] && nums[mid] > nums[mid + 1]
=> mid is a peak
2. nums[mid] > nums[mid - 1] && nums[mid] < nums[mid + 1]
=> There must exists a peak in right side
3. nums[mid] < nums[mid - 1] && nums[mid] > nums[mid + 1]
=> There must exists a peak in left side
4. nums[mid] < nums[mid - 1] && nums[mid] < nums[mid + 1]
=> Either in right or left side, there must exists a peak.
public class Solution {
public int findPeakElement(int[] nums) {
// Binary Search to find peak
int start = 0, end = nums.length - 1, mid = 0, prev = 0, next = 0;
while (start + 1 < end) {
mid = (end - start) / 2 + start;
prev = mid - 1;
next = mid + 1;
if (nums[mid] > nums[prev] && nums[mid] > nums[next])
return mid;
if (nums[mid] > nums[prev] && nums[mid] < nums[next]) {
start = mid;
continue;
}
if (nums[mid] < nums[prev] && nums[mid] > nums[next]) {
end = mid;
continue;
}
start = mid;
}
if (nums[start] > nums[end])
return start;
return end;
}
}
Find Peak Element 解答的更多相关文章
- [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] ≠ ...
- [LintCode] Find Peak Element 求数组的峰值
There is an integer array which has the following features: The numbers in adjacent positions are di ...
- lintcode 75 Find Peak Element
Hi 大家,这道题是lintcode上的find peak element的题,不是leecode的那道, 这两道题是有区别的,这道题的题目中说明了:只有左右两侧的数都小于某个元素,这种才是峰值, 而 ...
- 【leetcode】Find Peak Element
Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...
- Java for LeetCode 162 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
原题链接在这里:https://leetcode.com/problems/find-peak-element/ 题目: A peak element is an element that is gr ...
- 162. Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
随机推荐
- 除了修改WEBCONFIG会导致WEB服务重启外,还有其他的什么操作会导致重启?
1.修改WEBCONFIG文件 2.BIN文件夹下,添加.删除.覆盖文件 3.IIS应用程序池回收 参考文章:http://blog.csdn.net/hb_gx/archive/2007/05/ ...
- HDU 4276 The Ghost Blows Light
K - The Ghost Blows Light Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & ...
- 面试题32.从1到n整数中1出现的次数
题目:输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数.例如输入12,从 1到12这些整数中包含1的数字中1,10,11和12,1一共出现了5次 本题可以直接变量1到n的n个数然后分别计 ...
- 【hihoCoder第十六周】RMQ-ST算法
RMQ的大裸题.没什么意思.开始数组开小了,RE了一次.下面放代码. #include <bits/stdc++.h> using namespace std; vector<int ...
- c语言指针点滴1
#include <stdio.h> #include <stdlib.h> void main() { int *p = NULL;//指针开始最好都初始化为空 if(p = ...
- C# 合成图片
教师节快到了,给那些年的老师拼个图 前端有脸.眉.眼.特征.气泡等多元素图片 后端将最后选中元素的ID,合成“脸谱” /// <summary> /// 合并图片 /// </sum ...
- 与Jquery Mobile的第一次亲密接触
Jquery Mobile闻名已久,今天终于有亲密接触的机会. 通过动手写的demo,对它有了一个基本的认识: 自带的UI组件用起来简洁,方便:对旧版本的浏览器或移动设备能做到很好的优雅降级,而不影响 ...
- Makefile学习(三)执行make
9 执行make 一般方法:make. 某些情况:1.可能需要使用make更新一部分过时文件而不是全部 2.需要使用另外的编译器或者重新定义编译选项 3.只需要查看哪些文件被修改,不需要重新编译 所以 ...
- 安装VMware Sphere ESXi 5.5
安装VMware Sphere ESXi 5.5 1.准备 待安装ESXi 5.5的机器需要大于2GB以上内存,并且支持64位和虚拟化. 下载:VMware-VMvisor-Installer-5.5 ...
- Linux的用户和组
1. 查看配置文件/etc/shadow第一行中root账号的第三个字段(以':'分隔)中的数字,请算一下这个数字是怎么来的?距离1970年1月1日到上次更改密码的时间的间隔天数.例如root密码日期 ...