Maximum Erasure Value
Find the largest sum of a run of neighbouring numbers that holds no value twice.
Given an array of positive integers nums[2, 4, 5][4, 2, 4, 5, 6]nums
Example 1
Output: 17
Explanation: [2, 4, 5, 6] has no repeats and sums to 17.
Example 2
Output: 8
Explanation: [5, 2, 1] or [1, 2, 5].
The Idea
Picture a row of number tiles: 4, 2, 4, 5, 6. You may pick up one unbroken stretch of tiles and score the numbers on them, but the stretch may not hold the same number twice. Picking up 4 and 2 scores 6. Picking up all five tiles is not allowed, because that stretch holds two 4s. The best pick is 2, 4, 5, 6, worth 17.
How can we find that pick without trying every stretch? Start at the left end and grow a stretch one tile at a time, keeping a running sum. Every number is positive, so each tile that joins makes the sum bigger, and a stretch should grow for as long as it can. The only thing that stops it is a number that is already inside.
Two markers keep track of the stretch: rightleft
In the row labelled "2 has joined", the box holds 4 and 2, and the sum is 6. In the row labelled "the second 4 joins", the new 4 would repeat the first one, so the first 4 leaves from the left. The box now holds 2 and 4, and the sum is still 6. In the row labelled "6 has joined", 5 and 6 are in too, and the sum is 17, the answer.
Why drop tiles from the left, and not start a new stretch at the second 4? A stretch that ends at the second 4 and has no repeats cannot include the first 4, so it starts after it. But it can still include the 2, and starting over would throw that 2 away.
So both markers only ever move forward. rightleft
Solution Approach
Turning that idea into code means settling three things: how to tell whether a value is already in the window, how far left
Checking whether a value is in the window
How do we know that a new value is already inside? Scanning the whole window each time would be slow. A set of the values in the window answers in one step. Since every value is between 1 and 10,000, a boolean[10001]inWindowinWindow[v]v
The value is its own index, so there is no hashing to do, and the array is faster than Java's general-purpose HashSet
Moving the left edge
How far must left
So leftrightleft
Keeping the sum
The simple way is to try every starting position and add numbers until a repeat appears. For an array with no repeats at all, that adds about n²/2 numbers. The window does better by keeping one running sumbestsum
Can sumintint
Java Solution
The method maximumUniqueSubarray
- First, create the
array with 10,001 slots, and setinWindow ,left andsum to 0.best - A
loop movesfor from the first position to the last.right is the value about to join the window.nums[right] - Inside, a
loop runs as long aswhile is true. Each round clears the slot ofinWindow[nums[right]] , subtractsnums[left] fromnums[left] , and movessum one place right.left - Once the repeat is gone, set
to true, addinWindow[nums[right]] tonums[right] , and keep the larger ofsum andbest insum .best - When the
loop ends, returnfor .best
The main
public class MaximumErasureValue {
public static int maximumUniqueSubarray(int[] nums) {
boolean[] inWindow = new boolean[10001]; // values are between 1 and 10,000
int left = 0;
int sum = 0;
int best = 0;
for (int right = 0; right < nums.length; right++) {
while (inWindow[nums[right]]) { // remove from the left until the copy is gone
inWindow[nums[left]] = false;
sum -= nums[left];
left++;
}
inWindow[nums[right]] = true;
sum += nums[right];
best = Math.max(best, sum);
}
return best;
}
public static void main(String[] args) {
System.out.println(maximumUniqueSubarray(new int[] {4, 2, 4, 5, 6}));
System.out.println(maximumUniqueSubarray(new int[] {5, 2, 1, 2, 5, 2, 1, 2, 5}));
System.out.println(maximumUniqueSubarray(new int[] {3, 3, 3}));
System.out.println(maximumUniqueSubarray(new int[] {7}));
int[] all = new int[10000];
for (int i = 0; i < all.length; i++) {
all[i] = i + 1; // 1, 2, ..., 10000: no repeats at all
}
System.out.println(maximumUniqueSubarray(all));
}
}
Running it prints:
17
8
3
7
50005000
17 and 8 match the two examples. In [3, 3, 3][7]
The last line, 50005000, is 1 + 2 + ... + 10,000. Here the window takes in the whole array and leftint
Why a whileifif
Complexity
The work is one pass of rightleftright
- Time: O(n), because
brings each of the n numbers into the window once, andright takes each one out at most once. Theleft loop therefore runs at most n times in total, across all values ofwhile .right - Space: O(V), where V = 10,000 is the largest value allowed.
has one slot per possible value, however long the array is, and the rest isinWindow ,sum and the two markers.best