Minimum Operations to Make Array Non Decreasing - Solution & Explanation
Problem Statement
You are given an integer array nums of length n.
In one operation, you may choose any subarray nums[l..r] and increase each element in that subarray by x, where x is any positive integer.
Return the minimum possible sum of the values of x across all operations required to make the array non-decreasing.
An array is non-decreasing if nums[i] <= nums[i + 1] for all 0 <= i < n - 1.
Example 1:
Input: nums = [3,3,2,1]
Output: 2
Explanation:
One optimal set of operations:
- Choose subarray
[2..3]and addx = 1resulting in[3, 3, 3, 2] - Choose subarray
[3..3]and addx = 1resulting in[3, 3, 3, 3]
The array becomes non-decreasing, and the total sum of chosen x values is 1 + 1 = 2.
Example 2:
Input: nums = [5,1,2,3]
Output: 4
Explanation:
One optimal set of operations:
- Choose subarray
[1..3]and addx = 4resulting in[5, 5, 6, 7]
The array becomes non-decreasing, and the total sum of chosen x values is 4.
Constraints:
1 <= n == nums.length <= 1051 <= nums[i] <= 109
Approach Overview
Problem Overview: You are given an integer array and need the minimum number of operations required to transform it into a non‑decreasing sequence. A non‑decreasing array means nums[i] >= nums[i-1] for every index. The goal is to determine how many adjustments are required so every element respects this ordering.
Approach 1: Brute Force Increment Simulation (O(n * d) time, O(1) space)
The most direct idea is to scan the array from left to right and check whether the current element violates the non‑decreasing condition. If nums[i] < nums[i-1], repeatedly increment nums[i] until it becomes equal to nums[i-1]. Each increment counts as one operation. This simulation works but becomes inefficient when the difference between elements is large because every unit increment is processed individually. The logic is simple and useful for understanding the mechanics of the problem, but its runtime can degrade significantly.
Approach 2: Greedy Difference Accumulation (O(n) time, O(1) space)
A better observation removes the need for repeated increments. If nums[i] < nums[i-1], the minimum adjustment required is exactly nums[i-1] - nums[i]. Instead of simulating each step, add this difference directly to the operation count and treat the current element as if it has been raised to match nums[i-1]. This greedy idea works because increasing an element to the smallest valid value keeps future adjustments minimal. You process the array once, maintain the previous valid value, and accumulate required operations.
Approach 3: Prefix Maximum Tracking (O(n) time, O(1) space)
This variation frames the same greedy logic using a running prefix maximum. Maintain prev_max, the maximum value seen so far while scanning left to right. If the current number is smaller than prev_max, it must be raised to that value, contributing prev_max - nums[i] operations. Otherwise, update prev_max to the current value. Conceptually, you enforce the invariant that every element equals at least the maximum value to its left. This approach is easy to implement and commonly used in arrays and greedy style interview problems.
Recommended for interviews: The greedy single‑pass approach using a prefix maximum is the expected solution. Interviewers typically accept the brute‑force explanation as a starting point, but the optimized method shows you recognize the exact adjustment required without simulation. The final algorithm runs in linear time, uses constant space, and relies on a simple invariant maintained during iteration. Problems that enforce ordering constraints like this often appear in array processing and greedy strategy discussions.
Solution
We can traverse the array from left to right and calculate the difference between each pair of adjacent elements. If the current element is smaller than the previous one, we need to increase the current element so that it is at least equal to the previous element. The amount to increase is the difference between the previous element and the current element.
The time complexity is O(n), where n is the length of the array. The space complexity is O(1).
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Increment Simulation | O(n * d) | O(1) | Good for understanding the mechanics when differences between elements are small |
| Greedy Difference Accumulation | O(n) | O(1) | Best general solution; computes required increments directly |
| Prefix Maximum Tracking | O(n) | O(1) | Preferred interview implementation using a running maximum invariant |
Video Solution
Minimum Operations to Make Array Non-Decreasing | LeetCode 3914 | Subarray Increment Strategy • Techdose • 806 views views
Watch 8 more video solutions →Frequently Asked Questions
Is Minimum Operations to Make Array Non Decreasing easy or hard?
Minimum Operations to Make Array Non Decreasing Python/Java solution
How to solve Minimum Operations to Make Array Non Decreasing in O(n)?
What is the best approach for Minimum Operations to Make Array Non Decreasing?
Is Minimum Operations to Make Array Non Decreasing asked at Google/Amazon/Meta?
What data structure is used in Minimum Operations to Make Array Non Decreasing?
What is the time complexity of Minimum Operations to Make Array Non Decreasing?
Ready to solve this problem?
Practice Minimum Operations to Make Array Non Decreasing with our built-in code editor and test cases.
Practice on FleetCode