Minimum Number of Increments on Subarrays to Form a Target Array
Given an array of positive integers target and an array initial of same size with all zeros.
Return the minimum number of operations to form a target array from initial if you are allowed to do the following operation:
Choose any subarray from initial and increment each value by one.
The answer is guaranteed to fit within the range of a 32-bit signed 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.
classSolution {publicintminNumberOperations(int[] target) {// Each operation increments by 1int operationsUsed = target[0];int maxHeight = target[0];// Now we need to see upto where our current operations extend uptofor (int i =1; i <target.length; i++) {if (target[i] < maxHeight) maxHeight = target[i];// We need new operations for new local maximaselse { operationsUsed += target[i] - maxHeight; maxHeight = target[i]; } }return operationsUsed; }}