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] <= array[i + 1]
holds for every i
(1 <= i < n).
Example 1:
Input: [4,2,3] Output: True Explanation: You could modify the first4
to1
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].
Intuition
\nFor the given array , if we are changing at most one element , we should change to , as it would be guaranteed that , and would be the smallest possible to try and achieve .
\nAlgorithm
\nFor each possible change , check if the sequence is monotone increasing. We\'ll modify , a copy of the array .
\n\nComplexity Analysis
\nTime Complexity: Let be the length of the given array. For each element , we check if some sequence is monotone increasing, which takes steps. In total, this is a complexity of .
\nSpace Complexity: To hold our array , we need space.
\nIntuition
\nIf , then we may remove without changing the answer. Similarly, if , we may remove without changing the answer.
\nIf the problem is solvable, then after these removals, very few numbers will remain.
\nAlgorithm
\nConsider the interval corresponding to the subarray . When , we know we do not need to modify , and we can consider solving the problem on the interval instead. We use a similar approach for .
\nAfterwards, with the length of the interval under consideration being , if the interval has size 2 or less, then we did not find any problem.
\nIf our interval under consideration has 5 or more elements, then there are two disjoint problems that cannot be fixed with one replacement.
\nOtherwise, our problem size is now at most 4 elements, which we can easily brute force.
\n\nComplexity Analysis
\nTime Complexity: Let be the length of the given array. Our pointers and move at most times. Our brute force is constant time as there are at most 4 elements in the array. Hence, the complexity is .
\nSpace Complexity: The extra array only has at most 4 elements, so it is constant space, and so is the space used by our auxillary brute force algorithm. In total, the space complexity is .
\nIntuition
\nConsider all indices for which . If there are zero, the answer is True
. If there are 2 or more, the answer is False
, as more than one element of the array must be changed for to be monotone increasing.
At the problem index , we only care about the surrounding elements. Thus, immediately the problem is reduced to a very small size that can be analyzed by casework.
\nAlgorithm
\nAs before, let be the unique problem index for which . If this is not unique or doesn\'t exist, the answer is False
or True
respectively. We analyze the following cases:
Complexity Analysis
\nTime Complexity: Let be the length of the given array. We loop through the array once, so our time complexity is .
\nSpace Complexity: We only use and , and the answer itself as the additional space. The additional space complexity is .
\nAnalysis written by: @awice
\n