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.

解题思路1:

直接想到的思路是,由于num[-1]是负无穷,因此num[0]对于它的previous neighbor是上升的,只需从num头开始遍历,找到第一个num[i] > num[i + 1]的数,那么i就是Peak Element;

算法虽然看上去已经很高效,但是,时间复杂度o(n);

代码:

 class Solution {
public:
int findPeakElement(const vector<int> &num) {
int n = num.size(); for (int i = ; i < n; ++i) {
if (num[i - ] > num[i])
return i - ;
}
return n - ;
}
};

解题思路2:

o(n)的时间复杂度还是可以继续优化的,o(n)继续优化,那就是思考有没有o(logn)的算法,比较常见出现logn的算法模式是:分治算法,二分搜索等,都是一个思路,一次考察数列的一半;

由于题目中说,对于多个peak element,我们只需找到一个就可以了;同时,由于num[-1] = num[n] = -∞,因此数组中至少有一个peak element;那么设置begin、mid、end三个index分别指向数组的起始,中间和结尾:

1、如果num[mid] > num[mid + 1],说明在begin和mid 之间,至少存在一个peak element,因此只考察数组前半段就可以了;

2、如果num[mid] < num[mid + 1], 说明在mid + 1和end之间,至少存在一个peak element,因此只考察数组后半段就可以了;

(原数组相邻元素都不相等,不用考察相等的情况)

另,为什么选择mid和mid+1比,而不是mid和mid-1比,因为begin始终小于end(循环条件),而mid取(begin + end) / 2,mid有可能等于begin,因此mid+1一定存在,而mid-1有可能越界;

因此,代码:

 class Solution {
public:
int findPeakElement(const vector<int> &num) {
int begin = ;
int end = num.size() - ; while (begin < end) {
int mid = (begin + end) / ;
if (num[mid] > num[mid+])
end = mid;
else
begin = mid + ;
} return begin;
}
};

但是实际运行中,思路2并不比思路1快多少,因为思路2在数据集中才能有效果,如果在小数据集,思路2的操作步骤比比思路1还要多,因此可能更慢;

【Leetcode】【Medium】Find Peak Element的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. 【leetcode刷题笔记】Find Peak Element

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

  5. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  6. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  7. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

  8. 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number

    [Q7]  把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...

  9. 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters

    [Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...

  10. 【LeetCode算法题库】Day5:Roman to Integer & Longest Common Prefix & 3Sum

    [Q13] Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Valu ...

随机推荐

  1. Hive中 使用 Round() 的坑

    有个算法如下: SELECT MEMBERNUMBER, ROUND(SUM(SumPointAmount)) AS VALUE FROM BSUM_CRMPOINT WHERE UPPER(POIN ...

  2. JAVA学习2:Eclipse集成Maven

    我的环境: Eclipse:eclipse-jee-juno-SR2-win32 Maven:Maven3.0.5 1.Help->Eclipse Marketplace 2.选中要安装的插件, ...

  3. android 拍照和从相册选择组件

    android 拍照及从相册选择组件 单独封装到一个 activity 中便于更好的复用 拍照或从相册选择成功后使用 EventBus 发出广播回传图片路径,和调用者充分解耦合 根据传入参数支持裁剪和 ...

  4. c# 实现RPC框架的思路

    RPC框架,就是远程调用一个方法就像是本地调用一样. 用于网络消息的话,那么你的收发消息通过异步方法可以写在一起,很方便 核心代码 using System; using System.Collect ...

  5. ubuntu下安装Sublime Text2

    1.下载Sublime Text2官网下载地址:http://www.sublimetext.com 2.安装Sublime Text2解压即可使用,如linjiqin@ubuntu:~$ sudo ...

  6. <数据挖掘导论>读书笔记7 Apriori算法

    Apriori算法是一种最有影响的挖掘布尔关联规则频繁项集的算法.其核心是基于两阶段频集思想的递推算法.该关联规则在分类上属于单维.单层.布尔关联规则.在这里,所有支持度大于最小支持度的项集称为频繁项 ...

  7. golang学习之文件上传

    首先是上传页面upload.html: <!doctype html> <html> <head> <meta charset="utf-8&quo ...

  8. 修改django 后台admin用户的密码

    python manage.py shellfrom django.contrib.auth.models import User from django.contrib.auth.models im ...

  9. vim的多标签

    vim支持多标签页,可以在同一窗口同时打开多个文档, 两种方法: vim -d 通过vim --help后发现vim -d相当与vimdiff模式 例子: $ vim -d a.txt b.txt c ...

  10. easypoi导出单个sheet和多个sheet

    今天有时间研究了一下easypoi,感觉使用了easypoi导出excel方便了很多,不用写很多复杂的反射,只需要使用注解和一些工具类就可以实现常用的excel的导出,接下来介绍一下easypoi如何 ...