Given an array nums with n integers, your task is to check if it could become non-decreasing by modifying at most one element.
We define an array is non-decreasing if nums[i] <= nums[i + 1] holds for every i (0-based) such that (0 <= i <= n - 2).
Example 1:
Input: nums = [4,2,3] Output: true Explanation: You could modify the first 4 to 1 to get a non-decreasing array.
Example 2:
Input: nums = [4,2,1] Output: false Explanation: You cannot get a non-decreasing array by modifying at most one element.
Constraints:
n == nums.length1 <= n <= 104-105 <= nums[i] <= 105When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach for this problem involves checking every possible way to modify the given list of numbers. We'll try changing each number, one at a time, to see if it makes the list non-decreasing. It's like trying every single possible adjustment.
Here's how the algorithm would work step-by-step:
def non_decreasing_array_brute_force(numbers):
# Check if the original array is already non-decreasing
if is_non_decreasing(numbers):
return True
list_length = len(numbers)
for index_to_change in range(list_length):
original_value = numbers[index_to_change]
# Try changing the number to the value before it
if index_to_change > 0:
numbers[index_to_change] = numbers[index_to_change - 1]
if is_non_decreasing(numbers):
return True
# Try changing the number to the value after it
if index_to_change < list_length - 1:
numbers[index_to_change] = numbers[index_to_change + 1]
if is_non_decreasing(numbers):
return True
# Restore the original value before trying the next index
numbers[index_to_change] = original_value
return False
def is_non_decreasing(numbers):
for index in range(len(numbers) - 1):
# If a number is smaller than the previous, it's not non-decreasing
if numbers[index] > numbers[index + 1]:
return False
return TrueThe goal is to see if we can make a list of numbers be in non-decreasing order by changing at most one number. We'll walk through the list and if we find a problem, we'll try to fix it by changing one of the numbers involved in the problem.
Here's how the algorithm would work step-by-step:
def can_be_non_decreasing(number_list):
number_of_errors = 0
for index in range(len(number_list) - 1):
if number_list[index] > number_list[index + 1]:
# Found a non-decreasing violation
number_of_errors += 1
if number_of_errors > 1:
return False
# Attempt to correct the violation
if index > 0 and number_list[index - 1] > number_list[index + 1]:
# Modifying the next element could violate previous order
number_list[index + 1] = number_list[index]
else:
# Modify current element to fix the order.
number_list[index] = number_list[index + 1]
return True| Case | How to Handle |
|---|---|
| Empty array | Return true because an empty array is considered non-decreasing. |
| Array with one element | Return true because an array with one element is considered non-decreasing. |
| Array with two elements in non-decreasing order | Return true because it is already non-decreasing. |
| Array with two elements in decreasing order | Return true because we can modify one element to make it non-decreasing. |
| Array with all elements equal | Return true because the array is already non-decreasing. |
| Array with negative numbers and zeros | The comparison nums[i] <= nums[i + 1] works correctly with negative numbers and zeros. |
| Array where the first violation is near the beginning and requires modifying the earlier element | The algorithm needs to consider modifying either the i-th or (i+1)-th element, and choose the one that yields a non-decreasing array. |
| Array where the first violation is near the end and requires modifying the later element | The algorithm needs to correctly handle the boundary conditions when the violation occurs near the end of the array. |