Given an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.



We define an array is non-decreasing if array[i]  holds for every i (1 

Example 1:

Input: [4,2,3]
Output: True
Explanation: You could modify the first 4 to 1 to get a non-decreasing array.



Example 2:

Input: [4,2,1]
Output: False
Explanation: You can't get a non-decreasing array by modify at most one element.



Note:
The n belongs to [1, 10,000].

Difficulty: introductory

Starter code:

class Solution:
    def checkPossibility(self, nums: List[int]) -> bool:
        

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


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


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


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


class Solution:
     def checkPossibility(self, nums):
         """
         :type nums: List[int]
         :rtype: bool
         """
         idx=-1
         if len(nums)>=2:
             if nums[0]>nums[1]:
                 r=0
             else:
                 r=1
         for i in range(2,len(nums)):
             if nums[i]<nums[i-1]:
     #            idx=i-2
                 r=r-1
                 if nums[i]<nums[i-2]:
                     nums[i]=nums[i-1]
             # if idx>=0:
             #     pre=nums[idx]
             # else:
             #     pre=-2**32
             # if i<len(nums)-1 and nums[i+1]<pre:
             #     r=r-1
             # print(i,r)
             if r<0:
                 return False
         return True


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


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

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


class Solution:
     def checkPossibility(self, nums):
         """
         :type nums: List[int]
         :rtype: bool
         """
         
         n = len(nums)
         
         decreasing = []
         
         for i in range(n-1):
             if nums[i] > nums[i+1]:
                 decreasing.append(i)
                 
         if not decreasing:
             return True
         
         if len(decreasing) > 1:
             return False
         
         def check(array):
             n = len(array)
             for i in range(n-1):
                 if array[i] > array[i+1]:
                     return False
                 
             return True
         
         i = decreasing[0]
         
         temp = nums[i]
         nums[i] = nums[i+1]
         if check(nums):
             return True
         nums[i] = temp
         
         nums[i+1] = nums[i]
         if check(nums):
             return True
         
         return False           
         


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

class Solution:
     def checkPossibility(self, nums):
         """
         :type nums: List[int]
         :rtype: bool
         """
         return sum(list(map(lambda t: t[0] - t[1] < 0, zip(nums[1:], nums[:-1])))) <= 1 and sum(list(map(lambda t: t[0] - t[1] < 0, zip(nums[2:], nums[:-2])))) <= 1