Minimum Operations to Make the Array Increasing - Problem

You have an integer array nums that you want to transform into a strictly increasing sequence. The only operation allowed is to increment any element by 1.

Your goal is to find the minimum number of operations needed to make the array strictly increasing, where each element must be smaller than the next one: nums[i] < nums[i+1].

Example: If nums = [1, 2, 3], it's already strictly increasing, so 0 operations are needed. But if nums = [1, 1, 1], you need to make it [1, 2, 3], requiring 3 operations total.

Remember: You can only increase values, never decrease them, and single-element arrays are automatically valid!

Input & Output

example_1.py โ€” Basic Increasing Array
$ Input: nums = [1,1,1]
โ€บ Output: 3
๐Ÿ’ก Note: We need to transform [1,1,1] to [1,2,3]. Operations: increment nums[1] once (1โ†’2) and nums[2] twice (1โ†’3), totaling 3 operations.
example_2.py โ€” Partially Sorted
$ Input: nums = [1,5,2,4,1]
โ€บ Output: 14
๐Ÿ’ก Note: Transform to [1,5,6,7,8]. nums[2]: 2โ†’6 (4 ops), nums[3]: 4โ†’7 (3 ops), nums[4]: 1โ†’8 (7 ops). Total: 14 operations.
example_3.py โ€” Already Sorted
$ Input: nums = [8]
โ€บ Output: 0
๐Ÿ’ก Note: Single element arrays are automatically strictly increasing, so no operations needed.

Constraints

  • 1 โ‰ค nums.length โ‰ค 5000
  • 1 โ‰ค nums[i] โ‰ค 105
  • You can only increment elements, never decrement
  • Array must become strictly increasing: nums[i] < nums[i+1]

Visualization

Tap to expand
Building Height Enforcement VisualizationHeight: 1Height: 1Violation!Height: 3Height: 2Violation!Height: 5Fix: +1Fix: +2Original: [1, 1, 3, 2, 5] โ†’ Final: [1, 2, 3, 4, 5]Total Operations: 3 (1 + 2 = 3)
Understanding the Visualization
1
Inspection Start
Begin with the first building as reference
2
Check Violation
If next building is too short, calculate floors needed
3
Add Floors
Increment height to minimum required (previous + 1)
4
Continue Inspection
Move to next building and repeat process
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach works because fixing each violation with the minimum required increment ensures no future conflicts while minimizing total operations.
Asked in
Google 45 Amazon 38 Meta 28 Microsoft 22
89.2K Views
High Frequency
~15 min Avg. Time
1.8K 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