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.

&nbsp;
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] -&gt; [1,1,1,1] -&gt; [1,1,1,2] -&gt; [2,1,1,2] -&gt; [3,1,1,2]


Example 3:

Input: target = [3,1,5,4,2]
Output: 7
Explanation: [0,0,0,0,0] -&gt; [1,1,1,1,1] -&gt; [2,1,1,1,1] -&gt; [3,1,1,1,1] -&gt; [3,1,2,2,2] -&gt; [3,1,3,3,2] -&gt; [3,1,4,4,2] -&gt; [3,1,5,4,2].


&nbsp;
Constraints:


	1 &lt;= target.length &lt;= 105
	1 &lt;= target[i] &lt;= 105


class Solution:
    def minNumberOperations(self, nums: List[int]) -> int:
        res=nums[0]
        prev=nums[0]
        for i in range(1,len(nums)):
            res += max(0,nums[i]-prev)
            prev=nums[i]
        return res
// Imagine 3 cases
// Case 1. [3,2,1], we need 3 operations.
// Case 2. [1,2,3], we need 3 operations.
// Case 3. [3,2,1,2,3], we need 5 operations.
// What we need to add is actually the diff (cur - prev)
// Time complexity: O(N)
// Space complexity: O(1)
class Solution {
    public int minNumberOperations(int[] target) {
        int res = 0;
        int prev = 0;
        for (int cur : target) {
            if (cur > prev) {
                res += cur - prev;
            }
            prev = cur;
        }
        return res;
    }
}
class Solution {
public:
    int minNumberOperations(vector<int>& target) {
        int n=target.size();
        int pre=0, cnt=0;
        for(int i=0;i<n;i++)
        {
            if(target[i]>pre)
            cnt+=target[i]-pre;
            pre=target[i];
        }
        return cnt;
    }
};
var minNumberOperations = function(target) {
    let res = target[0];
    for(let g=1; g<target.length; g++){
        if(target[g] > target[g-1]){
            res = res + (target[g] - target[g-1]);
        }
        // for target[g] < target[g-1] we need not worry as its already been taken care by previous iterations
    }
    return res;
};
Minimum Number of Increments on Subarrays to Form a Target Array