Minimum Number of Increments on Subarrays to Form a Target Array - Problem

Imagine you're a game developer creating a terrain generator! You start with a completely flat landscape (all zeros) and need to build mountains and hills to match a target elevation map.

You are given an integer array target representing the desired elevation at each position. You start with an array initial of the same size, but all elements are initially 0 (flat ground).

In each operation, you can choose any contiguous subarray from your current landscape and raise the elevation by 1 across that entire range. Think of it as adding a layer of soil or rock across a continuous section.

Your goal is to find the minimum number of operations needed to transform your flat initial array into the target elevation map.

Example: If target = [1,2,3,2,1], you need to strategically choose subarrays to increment until you match this mountain-like shape with the fewest operations possible.

Input & Output

example_1.py โ€” Basic Mountain
$ Input: target = [1,2,3,2,1]
โ€บ Output: 3
๐Ÿ’ก Note: We need 3 operations: First operation covers entire array [0โ†’1,0โ†’1,0โ†’1,0โ†’1,0โ†’1]. Second operation covers middle section [1โ†’2,1โ†’2,1โ†’2]. Third operation targets the peak [2โ†’3]. This creates the mountain shape optimally.
example_2.py โ€” Plateau
$ Input: target = [3,1,1,2]
โ€บ Output: 4
๐Ÿ’ก Note: Start with 3 operations for the first element. Then we have drops and rises: 3โ†’1 (no extra ops), 1โ†’1 (no extra ops), 1โ†’2 (add 1 op). Total: 3 + 0 + 0 + 1 = 4 operations.
example_3.py โ€” Single Element
$ Input: target = [1,1,1,1]
โ€บ Output: 1
๐Ÿ’ก Note: Since all elements are the same height (1), we only need 1 operation to increment the entire array from [0,0,0,0] to [1,1,1,1].

Visualization

Tap to expand
12321Target: [1, 2, 3, 2, 1]Greedy Analysis1. Base height: 1 operation2. 1โ†’2: +1 operation3. 2โ†’3: +1 operation4. 3โ†’2: 0 operations5. 2โ†’1: 0 operationsTotal: 1+1+1 = 3Key Insight:Only height increasesrequire new operations!
Understanding the Visualization
1
Analyze Base Height
Start with target[0] operations to build the initial elevation
2
Process Increases
For each height increase, add new layer operations
3
Handle Decreases
Height decreases require no extra operations - layers naturally end
4
Sum Total
Total operations = base + all height increases
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach works because increment operations can be viewed as horizontal layers. When terrain height increases, we must start new layers. When it decreases, existing layers naturally terminate, requiring no additional operations.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the array

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using constant extra space

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค target.length โ‰ค 105
  • 1 โ‰ค target[i] โ‰ค 105
  • The answer is guaranteed to fit in a 32-bit integer
Asked in
Google 28 Amazon 22 Meta 15 Microsoft 12
28.4K Views
Medium Frequency
~25 min Avg. Time
1.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen