【Leetcode】【Medium】Find Peak Element
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的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【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 ...
- 【leetcode刷题笔记】Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【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 ...
- 【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 ...
- 【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 ...
- 【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 ...
- 【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 ...
随机推荐
- egg
简介 阿里的 红色的是项目名字 egg-init --type simple First && cd First egg-init --type simple Second &a ...
- Oracle TM锁和TX锁
CREATE TABLE "TEST6" ( "ID" ), "NAME" ), "AGE" ,), "SEX ...
- QQ游戏--捕鱼假日竞技港对抗岛自动刷贝壳辅助使用教程和下载地址
首先解压缩到D盘根目录 再进入buyujiari文件夹双击 然后打开QQ游戏,进入竞技港-->对抗岛,到达开始准备的界面 再打开 辅助.exe 360对按键精灵的一个文件会提示病毒,可不用理会 ...
- unity Socket TCP连接案例(一)
非常清晰的demo 服务端 using System; using System.Collections; using System.Collections.Generic; using System ...
- 二叉树数组C++实现
基本概念梳理 孩子:子结点 双亲:父节点 度:有多少个子结点 有序树:固定的排列的树 无序树:排列与顺序无关的树 二叉树:所有结点小于等于2的树 源代码:https://github.com/cjy5 ...
- Python爬虫-播报天气信息(生成exe文件)待续
#!/usr/bin/env python3 # -*- coding : utf-8 -*- '''1.从https://my.oschina.net/joanfen/blog/140364获取要播 ...
- 【关于eclipse的一些自己常用的插件】
代码自动走查: sonarlnt:
- 第五章 使用java实现面向对象异常
第五章 异常 一.异常概述 概述:异常是在程序的运行过程中所发生的不正常的事件,他会中断正在运行的程序 二.异常处理 1.关键字:try catch finally throw throws 2.Tr ...
- Spring boot Mybatis 整合(注解版)
之前写过一篇关于springboot 与 mybatis整合的博文,使用了一段时间spring-data-jpa,发现那种方式真的是太爽了,mybatis的xml的映射配置总觉得有点麻烦.接口定义和映 ...
- 互联网轻量级框架SSM-查缺补漏第三天
简言:平安夜,继续慵懒的学习.我真的是不喜欢学习··· 第三章认识MyBatis核心组件 3.1 持久层的概念和MyBatis的特点 持久层:可以将业务数据存储带磁盘,具有长期存储的能力.一般执行持久 ...