作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/peak-index-in-a-mountain-array/description/

题目描述

Let’s call an array A a mountain if the following properties hold:

  1. A.length >= 3
  2. There exists some 0 < i < A.length - 1 such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1]

Given an array that is definitely a mountain, return any i such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1].

Example 1:

Input: [0,1,0]
Output: 1

Example 2:

Input: [0,2,1,0]
Output: 1

Note:

  1. 3 <= A.length <= 10000
  2. 0 <= A[i] <= 10^6
  3. A is a mountain, as defined above.

题目大意

找出一个数组中的山峰的索引。山峰的意思是指在一个长度大于3的数组中,某个数值比相邻的两个数值都大。

解题方法

二分查找

这个一看就是二叉搜索啊,曾经的面试题,印象很深刻。

这个比普通二叉搜索的改进地方就是不再判断left, mid, right三者之间的关系。而是对于中间的mid,去判断mid和其相邻的两个元素的关系。根据是否符合山峰的上坡和下坡,去移动指针。

class Solution(object):
def peakIndexInMountainArray(self, A):
"""
:type A: List[int]
:rtype: int
"""
left, right = 0, len(A) - 1
while left < right:
mid = (left + right) / 2
if A[mid - 1] < A[mid] and A[mid] < A[mid + 1]:
left = mid
elif A[mid - 1] > A[mid] and A[mid] > A[mid + 1]:
right = mid
else:
break
return mid

二刷的时候,写了一个打败100%的写法,也就是我们使用了更少的判断。

class Solution:
def peakIndexInMountainArray(self, A):
"""
:type A: List[int]
:rtype: int
"""
N = len(A)
left, right = 0, N
while left < right:
mid = left + (right - left) // 2
if A[mid - 1] < A[mid] and A[mid] > A[mid + 1]:
return mid
if A[mid] < A[mid + 1]:
left = mid + 1
else:
right = mid
return -1

查找最大值位置

这个做法是在题目保证了给出的数组是满足条件的,那么只要找出最大值的位置那么一定就满足了条件。

class Solution:
def peakIndexInMountainArray(self, A):
"""
:type A: List[int]
:rtype: int
"""
return A.index(max(A))

寻找第一个下降的位置

题目给出的是满足条件的数组,所以找出第一个下降的位置就是题目所求啊。

class Solution:
def peakIndexInMountainArray(self, A):
"""
:type A: List[int]
:rtype: int
"""
for i in range(len(A) - 1):
if A[i + 1] < A[i]:
return i
return -1

日期

2018 年 6 月 17 日 —— 端午节也在刷题2333
2018 年 11 月 5 日 —— 打了羽毛球,有点累

【LeetCode】852. Peak Index in a Mountain Array 解题报告(Python)的更多相关文章

  1. LeetCode 852 Peak Index in a Mountain Array 解题报告

    题目要求 Let's call an array A a mountain if the following properties hold: A.length >= 3 There exist ...

  2. LeetCode 852. Peak Index in a Mountain Array C++ 解题报告

    852. Peak Index in a Mountain Array -- Easy 方法一:二分查找 int peakIndexInMountainArray(vector<int>& ...

  3. LeetCode 852. Peak Index in a Mountain Array (山脉数组的峰顶索引)

    题目标签:Binary Search 题目给了我们一组 int array,让我们找到数组的 peak. 利用 binary search, 如果数字比它后面那个数字小,说明还在上坡,缩小范围到右半边 ...

  4. LeetCode 852. Peak Index in a Mountain Array(C++)

    Let's call an array A a mountain if the following properties hold: A.length >= 3 There exists som ...

  5. leetcode 852. Peak Index in a Mountain Array

    Input: [0,1,0] Output: 1 Input: [0,2,1,0] Output: 1解: 比较数组中的i和i-1的大小,如果前一位大于后一位数字,前一位则是结果 let ans = ...

  6. 【Leetcode_easy】852. Peak Index in a Mountain Array

    problem 852. Peak Index in a Mountain Array solution1: class Solution { public: int peakIndexInMount ...

  7. 852. Peak Index in a Mountain Array

    class Solution { public: int peakIndexInMountainArray(vector<int>& A) { return max_element ...

  8. 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...

  9. Leetcode之二分法专题-852. 山脉数组的峰顶索引(Peak Index in a Mountain Array)

    Leetcode之二分法专题-852. 山脉数组的峰顶索引(Peak Index in a Mountain Array) 我们把符合下列属性的数组 A 称作山脉: A.length >= 3 ...

随机推荐

  1. Linux-设置终端界面的字体颜色和自定义常用快捷功能

    .bashrc是一个隐藏的文件,要打开并修改该文件需要: (0)命令:cd ~ (1)命令:ls -a 找到文件 .bashrc: (2) 命令 vim ~/.bashrc 进入到文件: (3) 直接 ...

  2. Scrapy-Splash的安装和使用

    Scrapy-Splash是一个Scrapy中支持JavaScript渲染的工具. Scrapy-Splash的安装分为两部分.一个是Splash服务的安装,具体是通过Docker,安装之后,会启动一 ...

  3. Spring 注解开发

    目录 注解开发简介 常用注解 启用注解功能 bean 定义:@Component.@Controller.@Service.@Repository bean 的引用类型属性注入:@Autowired. ...

  4. centos7 自动同步时间

    rm -rf /etc/localtime ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime vim /etc/sysconfig/cloc ...

  5. Rest使用get还是post

    1. get是从服务器上获取数据,post是向服务器传送数据. 2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到.post是通过 ...

  6. OpenStack之之一: 快速添加计算节点

    根据需求创建脚本,可以快速添加节点#:初始化node节点 [root@node2 ~]# systemctl disable NetworkManager [root@node2 ~]# vim /e ...

  7. 【MySQL】亲测可用的教程筛选:安装与卸载

    windows版本的 安装看这篇,非常详细:https://www.cnblogs.com/winton-nfs/p/11524007.html 彻底清除:https://www.pianshen.c ...

  8. linux 让.net 控制台后台运行

    命令     nohup 你的shell命令  & 例如    nohup dotnet  MQTTClient.dll & 输入完成后,终端会有提示 这时再按下回车 回到shell命 ...

  9. 了解C#的Expression

    我们书接上文,我们在了解LINQ下面有说到在本地查询IEnumerbale主要是用委托来作为传参,而解析型查询 IQueryable则用Expression来作为传参: public static I ...

  10. Kafka从入门到放弃(三) —— 详说生产者

    上一篇对Kafka做了简单介绍,还没看的朋友可以点击下方链接. Kafka从入门到放弃(一) -- 初识别Kafka 消息中间件必须与生产者和消费者一起存在才有意义,这次先来聊聊Kafka的生产者. ...