作者: 负雪明烛
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中对errno是EINTR的处理

    慢系统调用(slow system call):此术语适用于那些可能永远阻塞的系统调用.永远阻塞的系统调用是指调用有可能永远无法返回,多数网络支持函数都属于这一类.如:若没有客户连接到服务器上,那么服 ...

  2. 【模板】二分图最大匹配(匈牙利算法)/洛谷P3386

    题目链接 https://www.luogu.com.cn/problem/P3386 题目大意 给定一个二分图,其左部点的个数为 \(n\),右部点的个数为 \(m\),边数为 \(e\),求其最大 ...

  3. 【模板】Splay(伸展树)普通平衡树(数据加强版)/洛谷P6136

    题目链接 https://www.luogu.com.cn/problem/P6136 题目大意 需要写一种数据结构,来维护一些非负整数( \(int\) 范围内)的升序序列,其中需要提供以下操作: ...

  4. MySQL:事务常用语句

    Mysql(版本是8)的事务隔离级别 默认是RR:REPEATABLE-READ:可重复读 查看 当前隔离级别 全局隔离级别  修改 -- 当前修改 -- 设置成可重复读 SET transactio ...

  5. 容器之分类与各种测试(四)——map

    map和set的区别在于,前者key和value是分开的,前者的key不会重复,value可以重复:后者的key即为value,后者的value不允许重复.还有,map在插入时可以使用 [ ]进行(看 ...

  6. Linux基础命令---enable开启shell命令

    enable enable指令用来关闭或者激活shell内部命令.此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora. 1.语法       enable [-a]   ...

  7. Git命令行演练-团队开发

    ** 团队开发必须有一个共享库,这样成员之间才可以进行协作开发** ### 0. 共享库分类    > 本地共享库(只能在本地面对面操作)        - 电脑文件夹/U盘/移动硬盘    & ...

  8. mysql 报 'Host ‘XXXXXX’ is blocked because of many connection errors'

    1. 问题:服务启动时,日志报错,导致启动失败: Caused by: com.mysql.cj.exceptions.CJException: null,  message from server: ...

  9. 【编程思想】【设计模式】【行为模式Behavioral】策略模式strategy

    Python版 转自https://github.com/faif/python-patterns/blob/master/behavioral/strategy.py #!/usr/bin/env ...

  10. java中二维数组初始化的几种方法

    /* 第一种方式 */ int tdarr1[][] = { { 1, 3, 5 }, { 5, 9, 10 } }; /* 第二种方式 */ int tdarr2[][] = new int[][] ...