배열 추가문제

less than 1 minute read

Question: 유효한 팰린드롬 2

Given an integer array nums, return true if the given array is monotonic, or false otherwise. monotonic은 증가만 하거나 감소만 하는 리스트를 말한다.

입력: nums = [6,5,4,4]

출력: true

내 풀이

class Solution(object):
    def isMonotonic(self, A: List[int]):
        
        n = len(A)
        if n <= 2: 
            return True
				
        greater = False
        smaller = False
        for i in range(1, n):
            if A[i - 1] > A[i]:
                greater = True
            if A[i - 1] < A[i]:
                smaller = True

            if greater and smaller:
                return False

        return True
            
                

결과

A: Runtime: 1008 ms, faster than 73.39% of Python3 online submissions for Monotonic Array. Memory Usage: 28 MB, less than 97.48% of Python3 online submissions for Monotonic Array.

Comments