Problem · Array
Minimum Number of Increments on Subarrays to Form a Target Array
Learn this problemProblem statement
🌷 Heads up ->> Based on the original post, the actual question was VERY similar to the current question but not EXACT the same 🌻
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.
Function
minNumberOperations(target: int[]) → intExamples
Example 1
target = [1,2,3,2,1]return = 3We 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
target = [3,1,1,2]return = 4
- [0,0,0,0] -> [1,1,1,1] -> [1,1,1,2] -> [2,1,1,2] -> [3,1,1,2]
Example 3
target = [3,1,5,4,2]return = 7
- [0,0,0,0,0] -> [1,1,1,1,1] -> [2,1,1,1,1] -> [3,1,1,1,1] -> [3,1,1,1,2] -> [3,1,2,2,2] -> [3,1,3,3,2] -> [3,1,5,4,2].
Constraints
1 <= target.length <= 10^51 <= target[i] <= 10^5
More Amazon problems
- Resolve Task DependenciesONSITE INTERVIEW · Seen Jul 2026
- Shortest Distance on a Circular Bus RouteOA · Seen Jul 2026
- Longest Increasing Subsequence With Bounded Adjacent DifferenceONSITE INTERVIEW · Seen Jul 2026
- Search in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Jul 2026
- Sliding Window MaximumONSITE INTERVIEW · Seen Jul 2026
- Merge IntervalsOA · Seen Jul 2026
- Sort Bug Report FrequenciesOA · Seen Jul 2026
- Drone Delivery RouteOA · Seen Jul 2026