관리 메뉴

솜씨좋은장씨

[leetCode] 941. Valid Mountain Array (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 941. Valid Mountain Array (Python)

솜씨좋은장씨 2021. 1. 29. 00:10
728x90
반응형

Given an array of integers arr, return true if and only if it is a valid mountain array.

Recall that arr is a mountain array if and only if:

  • arr.length >= 3
  • There exists some i with 0 < i < arr.length - 1 such that:
    • arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
    • arr[i] > arr[i + 1] > ... > arr[arr.length - 1]

 

Example 1:

Input: arr = [2,1]
Output: false

Example 2:

Input: arr = [3,5,5]
Output: false

Example 3:

Input: arr = [0,3,2,1]
Output: true

Constraints:

  • 1 <= arr.length <= 104
  • 0 <= arr[i] <= 104

Solution

class Solution:
    def validMountainArray(self, A: List[int]) -> bool:
        incre_flag = True
        answer = True
        if len(A) < 3:
            answer = False
        else:
            for i in range(len(A)-1):
                if A[i] == A[i+1]:
                    answer = False
                    break
                elif A[i] > A[i+1]:
                    if i == 0:
                        answer = False
                        break
                    else:
                        incre_flag = False
                elif incre_flag == False and A[i] < A[i+1]:
                    answer = False
                    break
                elif i == len(A) - 2 and incre_flag:
                    answer = False
        return answer

 

 

SOMJANG/CODINGTEST_PRACTICE

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

Comments