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:
[2..3] and add x = 1 resulting in [3, 3, 3, 2][3..3] and add x = 1 resulting 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:
[1..3] and add x = 4 resulting 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] <= 109Problem 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.
Solutions for this problem are being prepared.
Try solving it yourself| 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 |
Practice Minimum Operations to Make Array Non Decreasing with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor