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. 关于安卓苹果手机安装证书抓https的关键步骤

    苹果有关键步骤!!!

  2. js实现手机号身份证等加星(*)号

    下面来为各位整理了一些关于js实现手机号身份证等加星(*)号代码了,在js不足时我们还补充了php实现手机号身份证等加星(*)号的函数,有兴趣的一起来看看.   有时候为了不让用户的手机号码和身份证号 ...

  3. python文件操作与字符编码

    知识内容: 1.文件对象与文件处理流程 2.基本操作 3.上下文管理 4.文件的修改与文件内光标的移动 5.字符编码 一.文件对象与文件处理流程 1.文件对象 (1)文件分类 按文件中数据的组织形式可 ...

  4. servlet里的过滤器filter

    过滤器的主要作用 1,任何系统或网站都要判断用户是否登录. 2,网络聊天系统或论坛,功能是过滤非法文字. 3,统一解决编码 怎么创建一个过滤器: 1,生成一个普通的class类,实现Filter接口( ...

  5. mysql开启查询日志功能

    1.开启查询日志  https://www.cnblogs.com/kerrycode/p/7130403.html MYsql 查询日志配置    mysql> show variables ...

  6. Spring MVC 重定向

    @RequestMapping("/testRedirect") public String testRedirect(){ System.out.println("te ...

  7. 前端-javascript-DOM(重点)文档对象模型

    1.DOM概念-文档对象模型 // 什么是DOM ? /* Document Object Model 文档对象模型 面向对象: 三个特性 封装 继承 多态 一个对象: 属性和方法 说 万事万物皆对象 ...

  8. 前端-CSS-11-Z-index

    ---- z-index 这个东西非常简单,它有四大特性,每个特性你记住了,页面布局就不会出现找不到盒子的情况. z-index 值表示谁压着谁,数值大的压盖住数值小的, 只有定位了的元素,才能有z- ...

  9. 8 python time$datetime

    1.表示时间的方式 (1)时间戳 时间戳(timestamp)的方式:通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量. 我们运行“type(time.time())” ...

  10. eclipse 注释字体不一致的问题

    eclipse中 1.解决注释的文字大小不一的情况 2.想让注释和代码大小不一样 3.win10系统下,设置Text Font时找不到Courier New字体 1.解决注释的文字大小不一的情况 打开 ...