Non Decreasing Array
Decide whether changing at most one number can put an array in non-decreasing order.
Take an array of integers numsnums
Example 1
Output: true
Explanation: change 4 to 1, and the array reads [1, 2, 3].
Example 2
Output: false
Example 3
Output: false
Explanation: the array goes down only once, from 4 to 2, but no single change fixes it.
The Idea
Read the numbers from left to right as the steps of a staircase. A non-decreasing array is a staircase that only goes up or stays level. A place where a number is smaller than the one before it, so the staircase goes down, is what we will call a drop.
Example 1, [4, 2, 3][4, 2, 1]
That holds for any two drops. If they do not share a number, one change cannot reach both of them. So a second drop always means the answer is false. But Example 3, [3, 4, 2, 3]
A drop can be fixed in two ways: make the left number smaller, or make the right number bigger. Look at the picture below.
The top row is nums
So fixing a drop is really about the numbers around it. Lowering the left number can only clash with the number in front of it. Raising the right number can clash with the numbers after it, which have not been looked at yet. That makes lowering the safer fix: it leaves the right number, and everything after it, as it was. Raising is the way out when lowering would clash.
Solution Approach
Turning that idea into code means settling three things: how to find the drops, which number to change at a drop, and how to make sure the change did not create a new drop.
Finding the drops
Why not try every position as the one to change? For each position you would pick a value and check the whole array again, which takes about n² comparisons for n numbers. One walk from left to right is enough instead. Compare each number with the one before it and count the drops. At the second drop, the answer is false.
Choosing which number to change
Which fix should a drop at position inums[i - 1]nums[i]nums[i - 1]nums[i]nums[i - 2]nums[i]i
Take [1, 4, 2, 3][1, 2, 2, 3][1, 4, 4, 3]
When lowering would clash, raise nums[i]nums[i - 1][5, 7, 1, 8][5, 7, 7, 8]
Checking the changed number
How do we know a raise did not create a new drop on the right? Write the new value into the array. The walk compares nums[i]nums[i - 2]
Java Solution
The method checkPossibility
- First, set a counter
to 0. Then loop withchanges from 1 to the end of the array, comparingi withnums[i] . Ifnums[i - 1] is at least as big, there is no drop, and the loop moves on.nums[i] - At a drop, add 1 to
. If it is now more than 1, this is the second drop, so return false.changes - If
is less than 2, ori is not bigger thannums[i - 2] , lowernums[i] tonums[i - 1] .nums[i] - Otherwise, raise
tonums[i] . The new value stays in the array, so the next round of the loop compares it with the number after it.nums[i - 1] - When the loop ends with at most one drop fixed, return true.
The main[1, 4, 2, 3][5, 7, 1, 8]
import java.util.Arrays;
public class NonDecreasingArray {
public static boolean checkPossibility(int[] nums) {
int changes = 0;
for (int i = 1; i < nums.length; i++) {
if (nums[i] >= nums[i - 1]) {
continue; // no drop here
}
if (++changes > 1) {
return false; // a second drop needs a second change
}
if (i < 2 || nums[i - 2] <= nums[i]) {
nums[i - 1] = nums[i]; // lower the left value: nothing after it moves
} else {
nums[i] = nums[i - 1]; // lowering would break nums[i - 2], so raise
}
}
return true;
}
static void check(int... nums) {
String before = Arrays.toString(nums);
boolean ok = checkPossibility(nums);
System.out.println(before + " -> " + ok + ", array now " + Arrays.toString(nums));
}
public static void main(String[] args) {
check(4, 2, 3);
check(4, 2, 1);
check(3, 4, 2, 3);
check(1, 4, 2, 3);
check(5, 7, 1, 8);
check(1, 2, 4, 5, 3);
check(1, 1, 1);
check(7);
}
}
Running it prints:
[4, 2, 3] -> true, array now [2, 2, 3]
[4, 2, 1] -> false, array now [2, 2, 1]
[3, 4, 2, 3] -> false, array now [3, 4, 4, 3]
[1, 4, 2, 3] -> true, array now [1, 2, 2, 3]
[5, 7, 1, 8] -> true, array now [5, 7, 7, 8]
[1, 2, 4, 5, 3] -> true, array now [1, 2, 4, 5, 5]
[1, 1, 1] -> true, array now [1, 1, 1]
[7] -> true, array now [7]
Look at the first line: [4, 2, 3][2, 2, 3]
The third line is Example 3. It ends as [3, 4, 4, 3]
The next two lines show each fix on its own: [1, 4, 2, 3][5, 7, 1, 8][1, 2, 4, 5, 3][1, 1, 1][7]
The original implementation,
Complexity
The method walks the array once and keeps only a counter.
- Time: O(n), for an array of n numbers, because the loop compares each number with the one before it once, makes at most one change, and stops early at a second drop.
- Space: O(1). The fix is written into
itself, and onlynums andchanges are added.i
Making a choice and writing it into the array, so that later steps see it, also solves