我们把符合下列属性的数组 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
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/peak-index-in-a-mountain-array
二分
class Solution:
def peakIndexInMountainArray(self, A: List[int]) -> int:
l,r = 0,len(A)-1
while l<=r:
mid = (l+r)>>1
if A[mid-1]<A[mid]<A[mid+1]: #上坡
l=mid+1
elif A[mid-1]>A[mid]>A[mid+1]: #下坡
r=mid-1
else:
return mid
暴力
class Solution:
def peakIndexInMountainArray(self, A: List[int]) -> int:
for i in range(len(A)):
if A[i+1]<A[i]:
return i
暴力
class Solution:
def peakIndexInMountainArray(self, A: List[int]) -> int:
i=0
while A[i]<A[i+1]:
i+=1
return i