You are given a 0-indexed integer array nums. In one step, remove all elements nums[i] where nums[i - 1] &gt; nums[i] for all 0 &lt; i &lt; nums.length.

Return the number of steps performed until nums becomes a non-decreasing array.

&nbsp;
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.


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 105
	1 &lt;= nums[i] &lt;= 109


class Solution:
    def totalSteps(self, nums: List[int]) -> int:
        st = []
        ans = 0
        for i in nums:
            t = 0
            while st and st[-1][0] <= i:
                t = max(t, st.pop()[1])
            x = 0 
            if st: 
                x = t+1 
            st.append([i, x])
            ans = max(ans, x)
        return ans
class Solution {
    
    
    
    public int totalSteps(int[] nums) {
        
        int n = nums.length;
        int ans = 0;
        
        Stack<Pair<Integer,Integer>> st = new Stack();
        
        st.push(new Pair(nums[n-1],0));
        
        
        for(int i=n-2;i>=0;i--)
        {
            int count = 0;
            
            while(!st.isEmpty() && nums[i] > st.peek().getKey())
            {
                count = Math.max(count+1 , st.peek().getValue() );
                st.pop();
            }
            
            ans = Math.max(ans , count);
            st.push(new Pair(nums[i],count));
        }
        
        return ans;
        
    }
}
class Solution {
public:
    using pi = pair<int, int>;
    int totalSteps(vector<int>& nums) {
        int N = nums.size();
        map<int, int> mp;
        vector<pi> del;    // stores pairs of (previous id, toDelete id)
        for (int i = 0; i < N; ++i) {
            mp[i] = nums[i];
            if (i+1 < N && nums[i] > nums[i+1])
                del.emplace_back(i, i+1);
        }

        int ans = 0;  // number of rounds
        while (!del.empty()) {
            ++ans;
            vector<pi> nxt;  // pairs to delete in the next round
            for (auto [i,j] : del) mp.erase(j);   // first, get rid of the required deletions
            for (auto [i,j] : del) {
                auto it = mp.find(i);
                if ( it == end(mp) || next(it) == end(mp) )   // if it's not in the map anymore,
                    continue;                   // OR if it's the last element, skip it
                auto itn = next(it);            // now compare against next element in the ordering
                if (it->second > itn->second)   
                    nxt.emplace_back(it->first, itn->first);  // add the (current id, toDelete id)
            }
            swap(nxt, del);  // nxt is the new del
        }
        return ans;

    }
};
var totalSteps = function(nums) {
    let stack = [],
        dp = new Array(nums.length).fill(0),
        max = 0

    for (let i = nums.length - 1; i >= 0; i--) {
        while (stack.length > 0 && nums[i] > nums[stack[stack.length - 1]]) {
            dp[i] = Math.max(++dp[i], dp[stack.pop()])
            max = Math.max(dp[i], max)
        }
        stack.push(i)
    }
    return max
};
Steps to Make Array Non-decreasing