You are given an integer array target. You have an integer array initial of the same size as target with all elements initially zeros.
In one operation you can choose any subarray from initial and increment each value by one.
Return the minimum number of operations to form a target array from initial.
The test cases are generated so that the answer fits in a 32-bit integer.
Example 1:
Input: target = [1,2,3,2,1] Output: 3 Explanation: We need at least 3 operations to form the target array from the initial array. [0,0,0,0,0] increment 1 from index 0 to 4 (inclusive). [1,1,1,1,1] increment 1 from index 1 to 3 (inclusive). [1,2,2,2,1] increment 1 at index 2. [1,2,3,2,1] target array is formed.
Example 2:
Input: target = [3,1,1,2] Output: 4 Explanation: [0,0,0,0] -> [1,1,1,1] -> [1,1,1,2] -> [2,1,1,2] -> [3,1,1,2]
Example 3:
Input: target = [3,1,5,4,2] Output: 7 Explanation: [0,0,0,0,0] -> [1,1,1,1,1] -> [2,1,1,1,1] -> [3,1,1,1,1] -> [3,1,2,2,2] -> [3,1,3,3,2] -> [3,1,4,4,2] -> [3,1,5,4,2].
Constraints:
1 <= target.length <= 1051 <= target[i] <= 105Problem Overview: You start with an array of zeros and can increment any subarray by 1 in a single operation. The goal is to construct the given target array using the minimum number of such operations.
Approach 1: Difference Array Method (O(n) time, O(1) space)
The key observation is that increments only matter when the array value increases compared to the previous index. If target[i] is greater than target[i-1], you must start additional increment operations equal to the difference. When the value decreases or stays the same, previously started operations already cover that segment. Iterate through the array once, compute max(0, target[i] - target[i-1]), and accumulate the result. Conceptually, this acts like a difference array where positive increases represent new subarray increments that must start at that position. The approach is extremely efficient and only tracks the previous value, making the space complexity constant.
This technique frequently appears in array transformation problems where operations propagate across ranges. Instead of simulating each increment operation, you compute how many operations must start at each index.
Approach 2: Greedy Incrementation (O(n) time, O(1) space)
The greedy interpretation views the target array as layers of increments stacked vertically. Every time the height increases compared to the previous position, a new layer must start. If the height drops, some layers simply end earlier. The minimum operations equal the sum of all positive height increases while scanning from left to right. Initialize the answer with target[0] since you must perform that many operations starting at index 0, then iterate and add target[i] - target[i-1] whenever the difference is positive.
This greedy insight avoids complex simulation or dynamic programming. It also connects to patterns seen in greedy and monotonic stack problems where only upward transitions create new work. Even though the problem is labeled Hard, the optimal solution is a simple linear pass once the correct observation is made.
Recommended for interviews: The greedy/difference-array reasoning is what interviewers expect. Explaining why only positive increases matter demonstrates strong problem decomposition. Showing a naive simulation first can help communicate understanding, but the optimal O(n) scan proves you can identify the structural pattern behind range increment operations.
To solve the problem optimally, we can use the concept of differences between consecutive elements in the target array. The number of operations required to achieve the target at each index is dictated by the difference between the current element and the previous element in the target array. By summing all the positive differences, we get the total number of operations needed.
Here's why this works: starting from the initial array (which is all zeros), each increment can be seen as targeting a 'range' of the array. The increments impose certain requirements for transitions between consecutive numbers. Therefore, the sum of necessary transitions gives us the minimum operations required.
The solution starts by identifying the minimum number of operations needed as the first element of the target since we have to create that amount starting from zero. As we iterate over the target array, if a position 'i' has a value larger than its previous position, it's necessary to increment the difference to account for it. The sum of those required transitions is our answer.
Time Complexity: O(n), where n is the length of the target array, since we iterate over the array once.
Space Complexity: O(1), since we only use a constant amount of extra space.
Another approach to solve this problem is using a greedy strategy that incrementally builds the target array value-by-value, choosing increments as needed to transition between values in the target. This method efficiently considers the necessary increments at each step by analyzing both the current and previous elements.
This is done by incrementally forming the target using the fewest number of operations required when seen as a series of number levels that need reaching.
In this C solution, the function iterates through the target array while maintaining a 'previous' value that starts at zero. For each target element, if it's greater than 'previous', the difference is added to operations, effectively counting increments needed.
Time Complexity: O(n), where n is the total number of elements in the target array.
Space Complexity: O(1), since we're using constant extra space.
We define f[i] as the minimum number of operations required to obtain target[0,..i], initially setting f[0] = target[0].
For target[i], if target[i] leq target[i-1], then f[i] = f[i-1]; otherwise, f[i] = f[i-1] + target[i] - target[i-1].
The final answer is f[n-1].
We notice that f[i] only depends on f[i-1], so we can maintain the operation count using just one variable.
The time complexity is O(n), where n is the length of the array target. The space complexity is O(1).
Similar problems:
| Approach | Complexity |
|---|---|
| Difference Array Method | Time Complexity: O(n), where n is the length of the target array, since we iterate over the array once. |
| Greedy Incrementation | Time Complexity: O(n), where n is the total number of elements in the target array. |
| Dynamic Programming | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Difference Array Method | O(n) | O(1) | Best general solution. Uses positive differences to count new subarray increments. |
| Greedy Incrementation | O(n) | O(1) | Interview-friendly explanation using height increases between adjacent elements. |
Minimum Operations to Make Array Equal to Target | Minimum Number of Increments|Leetcode 3229 & 1526 • codestorywithMIK • 13,106 views views
Watch 9 more video solutions →Practice Minimum Number of Increments on Subarrays to Form a Target Array with our built-in code editor and test cases.
Practice on FleetCode