Find Peak Element(ARRAY - Devide-and-Conquer)
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.
1st TRY
时间复杂度要达到O(logn)必须用分治法。
如果array[mid-1]大于array[mid],则左边的子数组array[start..mid-1]肯定有peak element(因为array[start]总是大于左边的元素);同样地,如果array[mid+1]大于array[mid],则由边的子数组array[mid+1..end]肯定有peak element(因为array[end]总是大于右边的元素)。
class Solution {
public:
int findPeakElement(const vector<int> &num) {
return binarySearch(num, , num.size()-);
}
int binarySearch(const vector<int> &num, int start, int end)
{
if(end - start <= )
{
if(num[start]>num[end]) return start;
else return end;
} int mid = (start+end) >> ;
if(num[mid] > num[mid+]) return binarySearch(num, start, mid);
else return binarySearch(num, mid+, end);
}
};
Result: Accepted
Find Peak Element(ARRAY - Devide-and-Conquer)的更多相关文章
- 162. Find Peak Element (Array; Divide-and-Conquer)
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- LeetCode153 Find Minimum in Rotated Sorted Array. LeetCode162 Find Peak Element
二分法相关 153. Find Minimum in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unkn ...
- [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] ≠ ...
随机推荐
- Parity 钱包启动配置
Parity. Ethereum Client. By Wood/Paronyan/Kotewicz/Drwięga/Volf et al. Copyright 2015, 2016, 2017, 2 ...
- [转]C#API 实现锁屏+关屏
http://www.cnblogs.com/1971ruru/archive/2010/05/20/1740216.html public Form1( bool aLock ) { if (aLo ...
- 6-3-1appium iOS
环境准备 brew install carthage npm i -g ios-deploy brew install libimobiledevice --HEAD brew install ide ...
- ubuntu14.04 login loop issue
无法进入图形界面的所有问题几乎都碰到了,可惜尝试所有办法,还是各种broken packages 等,无法重装 ubuntu-desktop 成功. 耽误了2天,果断决定重装系统算了..尽管很多软件, ...
- Java常用的加密解密类(对称加密类)
Java常用的加密解密类 原文转载至:http://blog.csdn.net/wyc_cs/article/details/8793198 原创 2013年04月12日 14:33:35 1704 ...
- [UE4]C++代码操作SplineMesh
转自:http://aigo.iteye.com/blog/2279503 void ARaceSpline::OnConstruction(const FTransform& Transfo ...
- boost实现日期时间格式化
#include <iostream> #include<thread> #include<chrono> #include<clocale> #inc ...
- Windows Event 事件
事件对象就像一个开关:它只有两种状态(开和关). 开状态:我们称其为“有信号” 关状态:我们称其为“无信号” 可以在一个线程的执行函数中创建一个事件对象,然后观察它的状态,如果是“无信号”就让该线程睡 ...
- 学习MongoDB 三: MongoDB无法启动的解决方法
一简介 我们之前介绍了MongoDB入门(安装与配置),我们今天在打开MongDB时,我们先运行cmd.exe进入dos命令界面,然后进入cd D:\mongodb\bin目录下,启动服务或者mon ...
- jq上下级元素查找方法
1.parent([expr]) 获取指定元素的所有父级元素 2.next([expr]) 获取指定元素的下一个同级元素 3.nextAll([expr]) 获取指定元素后面的所有同级元素 4.and ...