class Solution(object):
# 思路:
# 从左往右遍历A,分别设置计数器记录上坡up和下坡down的长度;
# 当不满足上坡了,就计数下坡;
# 当下坡也不满足了,记录当前山脉的长度=up+down+1
# 遍历时若遇到两个元素相等,则跳过。
def longestMountain(self, A):
"""
:type A: List[int]
:rtype: int
"""
# 遍历A的指针
i = 1
# 记录最长的山脉长度
highMount = 0
# 开始遍历
while i < len(A):
# 上下坡计数器,每次初值为0
up = down = 0
# 记录上坡长度
while i < len(A) and A[i - 1] < A[i]:
up += 1
i += 1
# 记录下坡长度
while i < len(A) and A[i - 1] > A[i]:
down += 1
i += 1
# 记录当前山脉长度
if up > 0 and down > 0:
highMount = max(highMount, up + down + 1)
# 若遍历中碰到相同的元素,则跳过
while i < len(A) and A[i] == A[i - 1]:
i += 1
return highMount