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. 9-16Jenkins-2定时任务

    定时任务 选择定时任务,点击 "?" 会弹出使用教程,但讨厌英语的人,自然选择现成的中文. 猪脚踏浪https://www.cnblogs.com/zsg88/p/9178625. ...

  2. linux下给php安装curl、gd(ubuntu)

    安装方法很简单,只需要一条命令. # sudo apt-get install curl libcurl3 libcurl3-dev php5-curl 恭喜,PHP5 cURL安装完毕.记得重启Ap ...

  3. 用javascript/jQuery给CKEditor取值/赋值

    CKEditor 是著名的 HTML 编辑器,IBM.Oracle.Adobe 等都在用.CKEditor 创建于 2003 年,其前身为 FCKEditor,在 2009 年的时候把“F”去掉了,更 ...

  4. git修改用户名和邮箱

    用户名和邮箱地址是本地git客户端的一个变量,不随git库而改变. 每次commit都会用用户名和邮箱纪录. 1.查看用户名和地址 git config user.name git config us ...

  5. CentOS 6.6下安装配置Tomcat环境

    本文转载至:http://www.linuxidc.com/Linux/2015-08/122234.htm 实验系统:CentOS 6.6_x86_64 实验前提:防火墙和selinux都关闭 实验 ...

  6. 用 tornado 做网站 (7)

    转自:http://wiki.jikexueyuan.com/project/start-learning-python/309.html 用 tornado 做网站 (7) 到上一节结束,其实读者已 ...

  7. Oracle函数中将参数放在模糊查询中

    --diagnosis_name like '%'||diagnosis_names||'%' create or replace function asdf(MIN_DATE IN varchar2 ...

  8. ASP.NET基于Redis的Provider库

    因为session基于本地cache,以前我们自己写分布式缓存,或者数据库存储,或者cookie加密存储,来保存用户状态信息,但较少的直接通过创建一个继承 SessionStateStoreProvi ...

  9. 一起KVM环境下windows7虚拟机异常死机(BSOD)的问题解决

    先说一下环境: 一.硬件 8台服务器做的超融合架构,软件存储池, 每台服务器是96G内存,两颗Intel(R) Xeon(R) CPU E5-2670 0 @ 2.60GHz,32线程. 每台服务器是 ...

  10. python+selenium+requests爬取我的博客粉丝的名称

    爬取目标 1.本次代码是在python2上运行通过的,python3的最需改2行代码,用到其它python模块 selenium 2.53.6 +firefox 44 BeautifulSoup re ...