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)的更多相关文章

  1. 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] ≠ ...

  2. 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 ...

  3. [LeetCode] Find Peak Element 求数组的局部峰值

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  4. LeetCode 162 Find Peak Element

    Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...

  5. Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  6. [LintCode] Find Peak Element 求数组的峰值

    There is an integer array which has the following features: The numbers in adjacent positions are di ...

  7. lintcode 75 Find Peak Element

    Hi 大家,这道题是lintcode上的find peak element的题,不是leecode的那道, 这两道题是有区别的,这道题的题目中说明了:只有左右两侧的数都小于某个元素,这种才是峰值, 而 ...

  8. 【leetcode】Find Peak Element

    Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...

  9. 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] ≠ ...

随机推荐

  1. XStream和Dom4j的区别

    对于搞技术的人来说,xml文件的处理应该并不陌生吧,先总述下,个人感觉XStream在处理XML文件和JavaBean对象互转时比较好,dom4j对常用的xml配置文件操作比较好点:首先,Dom4j ...

  2. table边框

    border-collapse 语法 border-collapse:separate | collapse | inherit 默认值:separate 取值 separate: 默认值.边框会被分 ...

  3. Java网络编程详解

    内容: 1.网络通信协议 2.UDP与TCP 3.UDP通信 4.TCP通信 5.网络编程总结 1.网络通信协议 (1)基本概念 网络:由多台计算机以及外部设备连接起来的一个系统,我们称之为网络 通信 ...

  4. 了解ES6

    内容: 1.ES6介绍及基础 2.模块.类和继承 3.ES6高级特性 4.Generator和Iterator 5.异步编程 6.函数相关 内容参考:<ES6 标准入门> ES6标准阅读链 ...

  5. node实现缓存

    内容: 1.缓存基本原理 2.node实现缓存 1.缓存基本原理 第一重要.缓存策略: cache-control:用于控制缓存,常见的取值有private.no-cache.max-age.must ...

  6. UVA127

    模拟游戏,规则如下把卡牌一张接一张,从左到右依次摊开,不可以重叠,每当某张卡片和左边(左边第三张)卡片匹配,它就能放到另外一张卡片上,匹配的规则是他们有一样的级别或者花色,在每次移动完成以后,还需要再 ...

  7. HTML5 Canvas ( 图片填充样式 ) fillStyle, createPattern

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. xe7 Unresolved external CSPIN.OBJ

    工程里打开使用了CSPIN控件的界面窗体,再编译就好了 [ilink32 Error] Error: Unresolved external 'Vcl::Controls::TControl::Set ...

  9. mysql 自动执行事件

    首先配置mysql的配置文件my.ini,  加上event_scheduler = 1  开启自动执行事件配置 demo drop event event_test; CREATE EVENT ev ...

  10. 机器学习入门-K-means算法

    无监督问题,我们手里没有标签 聚类:相似的东西聚在一起 难点:如何进行调参 K-means算法 需要制定k值,用来获得到底有几个簇,即几种类型 质心:均值,即向量各维取平均值 距离的度量: 欧式距离和 ...