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] ≠ ...
随机推荐
- POJ 2706 Painter
Painter Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3157 Accepted: 1962 Descripti ...
- ASP.NET Web Pages:WebGrid 帮助器
ylbtech-.Net-ASP.NET Web Pages:WebGrid 帮助器 1.返回顶部 1. ASP.NET Web Pages - WebGrid 帮助器 WebGrid - 众多有用的 ...
- MongoDB整库备份+整库导入
备份前检查: [root@Load29 tmp]# mongo localhost: MongoDB shell version: connecting to: localhost:/test Ser ...
- [UE4]UE4中的常见类
一.Actor:可以放在世界中物体 二.Pawn:可以接受Controller输入的Actor 三.Character:是一个可以行走.跑.跳等行为的Pawn 四.Controller:没有物理表现的 ...
- [UE4]代理事件(C++)
用宏定义类似格式: DECLARE_DELEGATE //普通代理 DECLARE_DYNAMIC_DELEGATE_TwoParams //动态代理 DECLARE_DYNAMIC_MULTICAS ...
- ETL编程模型(场景)
使用场景: ETL是一个处理过程. 多个数据源之间进行数据同步 1:n:一对多同步数据 n:1:多个数据源到一个目的段 m;n:多个数据源多个目的段 ========================= ...
- VSCode编辑器编写Python代码
如何用VSCode愉快的写Python https://code.visualstudio.com/ 在学习Python的过程中,一直没有找到比较趁手的第三方编辑器,用的最多的还是Python自带 ...
- 常用数据库4 mongodb
知识内容: 1.mongodb介绍与基本使用 2.mongodb操作 一.mongodb介绍与基本使用 1.mongodb介绍 Mongodb是一款强大,灵活,且易于扩展的通用型数据库.它能扩展出非常 ...
- systemctl管理系统配置、服务
systemctl daemon-reload \&& systemctl enable docker \&& systemctl start docker \& ...
- jar包双击执行引用外部包问题
大家都知道一个java应用项目可以打包成一个jar,当然你必须指定一个拥有main函数的main class作为你这个jar包的程序入口. 具体的方法是修改jar包内目录META-INF下的MANIF ...