Python Easy Understand Solution


  • 0

    To return True, there is at most one pair for nums[i] > nums[i+1].
    Then if nums[i - 1] <= nums[i + 1] or nums[i] <= nums[i + 2], return True.
    Otherwise return False

    def checkPossibility(self, nums):
            i = -1
            for j in range(len(nums) - 1):
                if nums[j] > nums[j + 1]:
                    if i >= 0:
                        return False
                    else:
                        i = j
            if i in [-1, 0, len(nums) - 2]:
                return True
            if nums[i - 1] <= nums[i + 1] or nums[i] <= nums[i + 2]:
                return True
            return False

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.