fbaquant / LeetCode

1 stars 0 forks source link

Monotonic Array #110

Open juneharold opened 3 months ago

juneharold commented 3 months ago

Check if array is monotone increasing or decreasing.

class Solution:
   def isMonotonic(self, nums: List[int]) -> bool:
       n = len(nums)
       increasing = decreasing = True
       for i in range(1, n):
           if nums[i] < nums[i-1]:
               increasing = False
           if nums[i] > nums[i-1]:
               decreasing = False
           if not increasing and not decreasing:
               return False
       return True