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. js代码的执行顺序及运算

    代码执行顺序:从上往下,一行一行的执行(也叫一个模块一个模块的执行) 变量的提升(它不是变量的功能,而是浏览器的功能) js代码如何执行? js代码执行前,浏览器会给他一个全局的环境 叫window, ...

  2. H-Index II @python

    Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize ...

  3. keras的Embedding层

    keras.layers.embeddings.Embedding(input_dim, output_dim, embeddings_initializer='uniform', embedding ...

  4. Hadoop2.0构成之HDFS2.0

    HDFS2.0之HA 主备NameNode: 1.主NameNode对外提供服务,备NameNode同步主NameNode元数据,以待切换: 2.主NameNode的信息发生变化后,会将信息写到共享数 ...

  5. ORACLE常用数值函数、转换函数、字符串函数介绍

    ORACLE常用数值函数.转换函数.字符串函数介绍. 数值函数: abs(m) m的绝对值 mod(m,n) m被n除后的余数 power(m,n) m的n次方 round(m[,n]) m四舍五入至 ...

  6. 在线聊天室 -onlinechat

    做了一个 在线聊天室,目前没什么人气,希望能被百度收录,给大家提供一个相对好的聊天环境 在线聊天室:http://www.3003soft.top/im

  7. 本地yum源快速创建

    1.建立挂载目录mkdir /rui 2.挂载iso到新建的/rui目录

  8. 应用层级时空记忆模型(HTM)实现对实时异常流时序数据检测

    应用层级时空记忆模型(HTM)实现对实时异常流时序数据检测 Real-Time Anomaly Detection for Streaming Analytics Subutai Ahmad SAHM ...

  9. leetcode344

    public class Solution { public string ReverseString(string s) { var list = s.Reverse(); StringBuilde ...

  10. WINRAR 自解压脚本命令及变量

    自解压脚本命令 Path=d:\ ;绝对路径 ;Path=.\在当前文件夹中创建 ;Path=在“Program Files”中创建 ;在当前文件夹创建,无语句 Setup=释放后运行 Presetu ...