Watch 8 video solutions for Steps to Make Array Non-decreasing, a medium level problem involving Array, Linked List, Stack. This walkthrough by Coding Decoded has 9,521 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a 0-indexed integer array nums. In one step, remove all elements nums[i] where nums[i - 1] > nums[i] for all 0 < i < nums.length.
Return the number of steps performed until nums becomes a non-decreasing array.
Example 1:
Input: nums = [5,3,4,4,7,3,6,11,8,5,11] Output: 3 Explanation: The following are the steps performed: - Step 1: [5,3,4,4,7,3,6,11,8,5,11] becomes [5,4,4,7,6,11,11] - Step 2: [5,4,4,7,6,11,11] becomes [5,4,7,11,11] - Step 3: [5,4,7,11,11] becomes [5,7,11,11] [5,7,11,11] is a non-decreasing array. Therefore, we return 3.
Example 2:
Input: nums = [4,5,7,7,13] Output: 0 Explanation: nums is already a non-decreasing array. Therefore, we return 0.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 109Problem Overview: You are given an integer array nums. In one step, remove every element nums[i] where nums[i] < nums[i-1]. The process repeats until the array becomes non‑decreasing. The goal is to compute how many steps this process takes.
Approach 1: Iterative Removal Simulation (O(n²) time, O(n) space)
This method directly simulates the rule described in the problem. Iterate through the array and mark elements where nums[i] < nums[i-1]. Build a new array that excludes those elements, then repeat the process until no more removals occur. Each iteration requires scanning the array and rebuilding it. In the worst case (for example, a strictly decreasing sequence), you remove only a few elements per round, which leads to up to n rounds and O(n²) total work. Space complexity is O(n) because you construct intermediate arrays. This approach is useful for understanding the mechanics of the problem but becomes slow for large inputs.
Approach 2: Monotonic Stack (O(n) time, O(n) space)
The optimized solution uses a stack to track elements and the number of steps before they disappear. Traverse the array from right to left. Maintain a decreasing monotonic stack that stores pairs of (value, stepsToRemove). When the current element is greater than the top of the stack, that means the smaller element will eventually be removed in a future round. Pop elements from the stack while they are smaller, and track the maximum steps required for those elements to disappear. Each pop represents a chain reaction of removals. The number of steps for the current element becomes maxSteps + 1. Push the result back onto the stack.
This works because every element can be pushed and popped at most once. The stack effectively compresses multiple removal rounds into a single pass. Instead of simulating each iteration, it calculates how long each element survives before being eliminated. The final answer is the maximum step value seen during processing. Time complexity is O(n) and space complexity is O(n). The technique is common in problems involving arrays and elimination chains, especially when using array traversal combined with monotonic structures.
Recommended for interviews: Interviewers typically expect the monotonic stack solution. The iterative simulation shows you understand the removal process but does not scale well. Implementing the stack approach demonstrates knowledge of elimination patterns, stack-based reasoning, and how to reduce repeated array passes into a linear-time algorithm.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Removal Simulation | O(n²) | O(n) | Good for understanding the removal process or implementing a straightforward brute-force simulation. |
| Monotonic Stack | O(n) | O(n) | Best choice for interviews and large inputs. Converts repeated removal rounds into a single linear scan. |