Minimum Number of Increments on Subarrays to Form a Target Array

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
Inputtarget = [1,2,3,2,1]
Output3
We need at least 3 operations: increment indices 0 to 4, then 1 to 3, then index 2 to form the target array.
Example 2
Inputtarget = [3,1,1,2]
Output4
The target can be formed in 4 operations by incrementing suitable subarrays as shown.

Constraints

  • 1 <= target.length <= 10^5
  • 1 <= target[i] <= 10^5
  • The input is generated such that the answer fits inside a 32 bit integer.

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate