山脉数组的峰顶索引

我们把符合下列属性的数组 A 称作山脉:

A.length >= 3
存在 0 < i < A.length - 1 使得A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1]
给定一个确定为山脉的数组,返回任何满足 A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1] 的 i 的值。

示例 1:

输入:[0,1,0]
输出:1
示例 2:

输入:[0,2,1,0]
输出:1

提示:

3 <= A.length <= 10000
0 <= A[i] <= 10^6
A 是如上定义的山脉

函数命名:

def peakIndexInMountainArray(self, A):
"""
:type A: List[int]
:rtype: int
"""

解答:

# 二分查找 时间复杂度logn的版本def peakIndexInMountainArray(mountain):    height = len(mountain)    low = 0    while low <= height:        mid = (height + low) // 2        if array[mid] > array[mid - 1] and array[mid] > array[mid + 1]:            print(mid)            break        elif array[mid] > array[mid + 1]:            height = mid - 1        else:            low = mid + 1

array = [0, 1, 0]peakIndexInMountainArray(array)array = [0, 2, 1, 0]peakIndexInMountainArray(array)array = [0, 1, 2, 3, 7, 6, 5, 4, 2, 1, 0]peakIndexInMountainArray(array)array = [0, 1, 2, 7, 6, 5, 4, 0]peakIndexInMountainArray(array)

原文地址:https://www.cnblogs.com/zbligang/p/10411470.html

时间: 2024-07-30 11:37:15

山脉数组的峰顶索引的相关文章

力扣——山脉数组的峰顶索引

我们把符合下列属性的数组 A 称作山脉: A.length >= 3 存在 0 < i < A.length - 1 使得A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1] 给定一个确定为山脉的数组,返回任何满足 A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1]

力扣(LeetCode) 852. 山脉数组的峰顶索引

我们把符合下列属性的数组 A 称作山脉: A.length >= 3 存在 0 < i < A.length - 1 使得A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1] 给定一个确定为山脉的数组,返回任何满足 A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1]

算法演练2:山脉数组的峰顶

这个leetcode题目是这样的: 这个数组的特点是会形成一个山峰,而题目要求返回这个山峰的索引. 一般的解法 如果按题意来想,很快就想到一个解决办法:把a[i]跟a[i+1]作比较,如果a[i+1]比a[i]小了,那就是到山峰了,返回i即可.这个算法需要遍历数组,一直找到开始变小的情况. 对于这个算法,可以这样实现(因为山峰一定不会在最后出现,所以不必担心A[i+1]会越界): int peakIndexInMountainArray(int* A, int ASize) { int i; f

js如何移除数组中指定索引的项

js如何移除数组中指定索引的项:在Array对象中有给定的函数可以删除数组中指定的元素,虽然非常好用,但是总感觉看不到摸不着的比较别扭,下面就分享一个自定义的删除数组指定索引值元素的函数,希望给大家一个全新的思路.代码实例如下: var array=[]; array[0]="蚂蚁部落一"; array[1]="蚂蚁部落二"; array[2]="蚂蚁部落三"; array[3]="蚂蚁部落四"; array[4]="

LabVIEW中数组的自动索引

我们在LabVIEW里面使用While或者是For循环结构的时候,就会发现每一个循环中在它们的循环结构的边界都可以自动完成一个数组元素的索引或累积.LabVIEW中循环结构的这种能力就叫做自动索引(Auto-indexing). 不过有一点需要注意:LabVIEW中For循环中自动索引功能是默认启用的,而While循环中自动索引功能是默认关闭的. 下图中就是一个使用For循环实现自动索引功能的例子: 在这里我们可以看到,在For循环中,每次循环就创建了数组的一个元素,当整个循环结束之后,这个由随

python运算学习之Numpy ------ 数组的切片索引与循环遍历、条件和布尔数组、

数组的切片索引: 数组的切片索引和列表非常类似,下面用代码简单说明 1 a = np.random.rand(16).reshape(4, 4) 2 print("数组a:\n", a) 3 print(a[-1][1:4]) 4 Out[1]: 5 数组a: 6 [[0.04175379 0.43013992 0.5398909 0.40638248] 7 [0.3305902 0.11958799 0.48680358 0.30755734] 8 [0.00893887 0.384

[Swift Weekly Contest 111]LeetCode941. 有效的山脉数组 | Valid Mountain Array

Given an array A of integers, return true if and only if it is a valid mountain array. Recall that A is a mountain array if and only if: A.length >= 3 There exists some i with 0 < i < A.length - 1 such that: A[0] < A[1] < ... A[i-1] < A[

寻找数组的中心索引

Given an array of integers nums, write a method that returns the "pivot" index of this array. We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the ind

LeetCode724 寻找数组的中心索引

给定一个整数类型的数组 nums,请编写一个能够返回数组“中心索引”的方法. 我们是这样定义数组中心索引的:数组中心索引的左侧所有元素相加的和等于右侧所有元素相加的和. 如果数组不存在中心索引,那么我们应该返回 -1.如果数组有多个中心索引,那么我们应该返回最靠近左边的那一个. 示例 1: 输入: nums = [1, 7, 3, 6, 5, 6] 输出: 3 解释: 索引3 (nums[3] = 6) 的左侧数之和(1 + 7 + 3 = 11),与右侧数之和(5 + 6 = 11)相等. 同