A really easy Python solution with explanation


  • 0
    G

    For this problem, We should count how many times the items of the array have been changed. So my idea is to compare nums[i] with nums[i-1] and if nums[i] < nums[i-1] then set nums[i-1] with nums[i] . However the problem is not that easy, some conditions should take into consideration, for example

    • Condition One: nums[i] > nums[i-2]
      i-2 i-1   i 
    1   2   8   4 
    

    Here nums[i] > nums[i-2], so replace nums[i-1] with nums[i], that is nums[i-1] = nums[i]

    • Condition Two: nums[i] < nums[i-2]
      i-2 i-1   i 
    1   3   3   2   4
    

    Here nums[i] < nums[i-2], so replace nums[i] with nums[i-1], that is nums[i] = nums[i-1]

    As you can see, the key problem is which item should be replaced nums[i] or nums[i-1] and what value should be set.

    Here is the code

    class Solution(object):
        def checkPossibility(self, nums):
            """
            :type nums: List[int]
            :rtype: bool
            """
            change_count = 0
            for i in range(1, len(nums)):
                if nums[i] < nums[i-1]:
                    if i-1 >= 1:
                        if nums[i] >= nums[i-2]:
                            nums[i-1] = nums[i]
                        else:
                            nums[i] = nums[i-1]
                        change_count += 1
                    else:
                        nums[i-1] = nums[i]
                        change_count += 1
                        
                    if change_count >= 2:
                        return False
    
            return True
    
    

Log in to reply
 

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