Make Array Strictly Increasing
Make an array strictly increasing by replacing values with ones from a second array, as few times as possible.
You are given two integer arrays, arr1arr2arr1arr2arr2arr1
Example 1
Output: 1
Explanation: replace 5 with 2 to get [1, 2, 3, 6, 7].
Example 2
Output: 2
Explanation: replace 5 with 3 and 3 with 4 to get [1, 3, 4, 6, 7].
Example 3
Output: -1
Explanation: no set of replacements makes the array strictly increasing.
The Idea
Think of arr1arr2
Look at Example 1. In [1, 5, 3, 6, 7]arr2 = [1, 3, 2, 4]
Example 2 has the same arr1arr2 = [4, 3, 1]
Notice what the next number cares about. It only has to be larger than the last number placed, so the numbers before that no longer matter. After each number, it is enough to know two things: the last value so far, and how many replacements it took to get there. Let us call that pair a state. If two different ways end in the same last value, we keep the one with fewer replacements.
Look at the picture below. It follows Example 2, one value of arr1
We start from nothing yet, with 0 replaced. The 1 can be kept, giving last 1. At the 5 there are two states: keep it for last 5 with 0 replaced, or replace it with 3 for last 3 with 1 replaced. The state last 5 looks cheaper, but it is a dead end. The next value, 3, is not above 5, and arr2
From last 3, the value 3 must be replaced, with 4. After that, 6 and 7 can be kept. The orange boxes in the picture mark this cheapest way through, and it ends with 2 replaced, the answer.
Why replace 5 with 3, and not with 4? A smaller last value is never worse: any number that fits after 4 also fits after 3. So a replacement always takes the smallest value of arr2arr1
Solution Approach
Turning that idea into code means settling four things: what to remember after each number, which value a replacement uses, how to find that value quickly, and where to start and stop.
What to remember after each number
Why not try every mix of keeps and replacements? Each number can be kept or replaced, so 5 numbers give 32 mixes and 40 numbers give over a trillion. The states avoid that, because many mixes end in the same last value and only the cheapest one is kept.
The states fit in a map from last value to the fewest replacements. How big can it get? A last value is either the number of arr1arr2arr2
Which value a replacement uses
From a state with last value lastarr2last
Finding that value quickly
How do we find the smallest value above lastarr2TreeSethigher(x)xnullarr2
Where to start and when to stop
What is the state before any number is placed? Its last value must be smaller than every number, so that the first number can always be kept. Integer.MIN_VALUEint
If at some number no state can keep it and none can replace it, the map ends up empty and the answer is -1. Otherwise, after the last number, the answer is the smallest count among the states left.
Java Solution
The method makeArrayIncreasing
- First, put the values of
into aarr2 namedTreeSet , so the smallest value above any number is onepool call away.higher - Create the map
, from last value to fewest replacements, with one entry:states with 0, for nothing placed yet.Integer.MIN_VALUE - A loop takes each value of
in turn asarr1 , and starts an empty mapa for the states after it. Inside, a second loop goes over every entry ofnext , with its last valuestates and its countlast .ops - If
is larger thana , keeping it gives the statelast witha . Thenops =replacement , and if it exists, replacing gives the statepool.higher(last) withreplacement . Both go intoops + 1 withnext , which keeps the smaller count when the last value is already there.merge(..., Math::min) - After the second loop, if
is empty, return -1. Otherwisenext becomesnext for the next value.states - When all of
is done, return the smallest count inarr1 .states
The main[1, 5, 3][5, 4, 3][0, 1]arr2
import java.util.HashMap;
import java.util.Map;
import java.util.TreeSet;
public class MakeArrayStrictlyIncreasing {
public static int makeArrayIncreasing(int[] arr1, int[] arr2) {
TreeSet<Integer> pool = new TreeSet<>();
for (int v : arr2) {
pool.add(v);
}
// last value of the numbers placed so far -> fewest replacements to get there
Map<Integer, Integer> states = new HashMap<>();
states.put(Integer.MIN_VALUE, 0); // nothing placed yet
for (int a : arr1) {
Map<Integer, Integer> next = new HashMap<>();
for (Map.Entry<Integer, Integer> e : states.entrySet()) {
int last = e.getKey();
int ops = e.getValue();
if (a > last) {
next.merge(a, ops, Math::min); // keep a
}
Integer replacement = pool.higher(last); // smallest value above last
if (replacement != null) {
next.merge(replacement, ops + 1, Math::min); // replace a
}
}
if (next.isEmpty()) {
return -1; // no way to place a
}
states = next;
}
int best = Integer.MAX_VALUE;
for (int ops : states.values()) {
best = Math.min(best, ops);
}
return best;
}
public static void main(String[] args) {
System.out.println(makeArrayIncreasing(new int[] {1, 5, 3, 6, 7}, new int[] {1, 3, 2, 4}));
System.out.println(makeArrayIncreasing(new int[] {1, 5, 3, 6, 7}, new int[] {4, 3, 1}));
System.out.println(makeArrayIncreasing(new int[] {1, 5, 3, 6, 7}, new int[] {1, 6, 3, 3}));
System.out.println(makeArrayIncreasing(new int[] {1, 5, 3}, new int[] {2}));
System.out.println(makeArrayIncreasing(new int[] {5, 4, 3}, new int[] {1, 2, 3}));
System.out.println(makeArrayIncreasing(new int[] {0, 1}, new int[] {}));
}
}
Running it prints:
1
2
-1
1
2
0
The first three lines, 1, 2 and -1, match the examples. Example 3 fails at the 6: by then the only state left is last 6 with 1 replaced, reached by replacing 3 with 6. The 6 is not above 6, and arr2
The fourth line is 1 for [1, 5, 3]arr2 = [2][5, 4, 3][1, 2, 3]
Look at the last line: 0 for [0, 1]Integer.MIN_VALUEarr2
Complexity
The work is one pass over arr1
- Time: O(n × m × log m), where n is the length of
and m ofarr1 . Each of the n numbers visits at most m + 1 states, and each visit makes onearr2 call, about log m steps. Building thehigher adds O(m log m) once.TreeSet - Space: O(m). The
holds at most m values, and each map holds at most m + 1 states.TreeSet
Keeping only the smallest possible last value is also the key idea in