Skip to content

Latest commit

 

History

History
38 lines (26 loc) · 668 Bytes

845.md

File metadata and controls

38 lines (26 loc) · 668 Bytes

Longest Mountain in Array

Description

link


Solution

See code (Must first up and then down)


Code

O(n) O(1)

class Solution:
    def longestMountain(self, A):
        """
        :type A: List[int]
        :rtype: int
        """
        up = down = res = 0
        
        for i in range(len(A) - 1):
            if down and A[i + 1] > A[i] or A[i + 1] == A[i]:
                up = down = 0
            
            up += A[i + 1] > A[i]
            down += A[i + 1] < A[i]
            if up and down: res = max(res, up + down + 1)
        
        return res