본문 바로가기

IT/코딩테스트

[Leetcode] 941. Valid Mountain Array

728x90

https://leetcode.com/problems/valid-mountain-array/

class Solution:
    def validMountainArray(self, arr: List[int]) -> bool:

        increasing = True
        change = False

        if len(arr) >= 2 and arr[0] - arr[1] > 0:
            increasing = False

        before = arr[0]
        for i in arr[1:]:
            if increasing:
                if i > before:
                    before = i
                elif i < before:
                    before = i
                    increasing = False
                    change = True
                else:
                    return False
            else:
                if i < before:
                    before = i
                else:
                    return False

        return True if change else False
728x90