You are given a 0-indexed integer array nums. In one operation, you can:
i in the range 0 <= i < nums.lengthnums[i] to nums[i] + 1 or nums[i] - 1Return the minimum number of operations to make nums non-decreasing or non-increasing.
Example 1:
Input: nums = [3,2,4,5,0] Output: 4 Explanation: One possible way to turn nums into non-increasing order is to: - Add 1 to nums[1] once so that it becomes 3. - Subtract 1 from nums[2] once so it becomes 3. - Subtract 1 from nums[3] twice so it becomes 3. After doing the 4 operations, nums becomes [3,3,3,3,0] which is in non-increasing order. Note that it is also possible to turn nums into [4,4,4,4,0] in 4 operations. It can be proven that 4 is the minimum number of operations needed.
Example 2:
Input: nums = [2,2,3,4] Output: 0 Explanation: nums is already in non-decreasing order, so no operations are needed and we return 0.
Example 3:
Input: nums = [0] Output: 0 Explanation: nums is already in non-decreasing order, so no operations are needed and we return 0.
Constraints:
1 <= nums.length <= 10000 <= nums[i] <= 1000
Follow up: Can you solve it in O(n*log(n)) time complexity?
Problem Overview: You are given an integer array and can change any element to any value with a cost equal to the absolute difference. The goal is to make the array either non-decreasing or non-increasing while minimizing the total modification cost.
Approach 1: Dynamic Programming with Value Compression (O(n * m) time, O(m) space)
This approach treats the problem as a dynamic programming optimization. First collect all possible candidate values (usually the sorted version of the array). For each position i and candidate value v, compute the minimum cost to make the prefix valid if nums[i] becomes v. To maintain the non-decreasing constraint, transitions only come from candidate values ≤ v. Maintain prefix minimums to speed up the transition. Each state adds abs(nums[i] - v) to the previous minimum cost. Repeat the same process for the reversed array to simulate the non-increasing case.
This method is conceptually straightforward and useful when explaining the structure of the problem. However, the number of candidate values m can be up to n, giving O(n * m) time, which becomes slower for large inputs.
Approach 2: Greedy with Priority Queue (Optimal) (O(n log n) time, O(n) space)
A more efficient solution uses a greedy strategy with a max heap from greedy algorithms and a priority queue. Iterate through the array while maintaining a max heap of previously chosen values. Push each number into the heap. If the maximum element in the heap becomes larger than the current number, the non-decreasing constraint is violated. Fix it by lowering that previous value to the current number. The cost added is heap_top - current. Pop the top element and insert the corrected value.
The intuition is that when a previous element is too large, decreasing the largest one produces the smallest possible cost increase. The heap efficiently tracks which earlier element causes the biggest violation. This greedy correction guarantees minimal total adjustment cost.
To handle the non-increasing case, apply the same algorithm after reversing the array or by negating values. Compute both costs and return the minimum.
Recommended for interviews: Start by describing the dynamic programming formulation because it clearly models the constraint. Then present the greedy heap optimization. Interviewers typically expect the O(n log n) heap solution since it demonstrates pattern recognition and efficient constraint handling.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Dynamic Programming with Value Compression | O(n * m) | O(m) | When explaining the full DP formulation or when candidate values are small |
| Greedy with Max Heap (Priority Queue) | O(n log n) | O(n) | Best general solution; handles large arrays efficiently |
| Greedy Heap on Reversed Array | O(n log n) | O(n) | Used to compute the non-increasing transformation cost |
2263. Make Array Non-decreasing or Non-increasing (Leetcode Hard) • Programming Live with Larry • 641 views views
Practice Make Array Non-decreasing or Non-increasing with our built-in code editor and test cases.
Practice on FleetCode