Problem

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 atarget array frominitial.

The test cases are generated so that the answer fits in a 32-bit integer.

Examples

Example 1

1
2
3
4
5
6
7
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

1
2
3
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

1
2
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 <= 10^5
  • 1 <= target[i] <= 10^5

Solution

Method 1 – Greedy (Increment by Difference)

Intuition

Each increment operation can increase a subarray by 1. To minimize operations, at each position, only increment by the difference between the current and previous value (or from zero for the first element).

Approach

  1. Initialize ans = target[0].
  2. For each i from 1 to n-1, add max(target[i] - target[i-1], 0) to ans.
  3. Return ans.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include <vector>
using namespace std;
class Solution {
public:
    int minNumberOperations(vector<int>& target) {
        int ans = target[0];
        for (int i = 1; i < target.size(); ++i)
            ans += max(target[i] - target[i-1], 0);
        return ans;
    }
};
1
2
3
4
5
6
7
8
9
func minNumberOperations(target []int) int {
    ans := target[0]
    for i := 1; i < len(target); i++ {
        if target[i] > target[i-1] {
            ans += target[i] - target[i-1]
        }
    }
    return ans
}
1
2
3
4
5
6
7
8
class Solution {
    public int minNumberOperations(int[] target) {
        int ans = target[0];
        for (int i = 1; i < target.length; ++i)
            ans += Math.max(target[i] - target[i-1], 0);
        return ans;
    }
}
1
2
3
4
5
6
7
8
class Solution {
    fun minNumberOperations(target: IntArray): Int {
        var ans = target[0]
        for (i in 1 until target.size)
            ans += maxOf(target[i] - target[i-1], 0)
        return ans
    }
}
1
2
3
4
5
6
class Solution:
    def minNumberOperations(self, target: list[int]) -> int:
        ans = target[0]
        for i in range(1, len(target)):
            ans += max(target[i] - target[i-1], 0)
        return ans
1
2
3
4
5
6
7
8
9
impl Solution {
    pub fn min_number_operations(target: Vec<i32>) -> i32 {
        let mut ans = target[0];
        for i in 1..target.len() {
            ans += (target[i] - target[i-1]).max(0);
        }
        ans
    }
}
1
2
3
4
5
6
7
8
class Solution {
    minNumberOperations(target: number[]): number {
        let ans = target[0];
        for (let i = 1; i < target.length; ++i)
            ans += Math.max(target[i] - target[i-1], 0);
        return ans;
    }
}

Complexity

  • ⏰ Time complexity: O(n) — n = length of target.
  • 🧺 Space complexity: O(1) — only a few variables.