Steps to Make Array Non-decreasing LeetCode Solution

Problem – Steps to Make Array Non-decreasing LeetCode 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 <= 105
  • 1 <= nums[i] <= 109

Steps to Make Array Non-decreasing LeetCode Solution in Python

class Solution:
    def totalSteps(self, nums: List[int]) -> int:
        ans = 0
        nums.reverse()
        lst = [[nums[0], 0]]
        for i in range(1, len(nums)):
            cnt = 0
            while lst and lst[-1][0] < nums[i]:
                cnt = max(cnt + 1, lst[-1][1])
                lst.pop()
            lst.append([nums[i], cnt])
            ans = max(ans, cnt)
        return ans

Steps to Make Array Non-decreasing LeetCode Solution in C++

class Solution {
public:
    int totalSteps(vector<int>& nums) 
    {
        int n=nums.size(),i,ans=0,cnt,prev;
        stack <pair<int,int>> st;
        st.push({nums[n-1],0});
        for(i=n-2;i>=0;i--)
        {
            cnt=0;
            while(!st.empty() && nums[i]>st.top().first)
            {
                cnt=max(cnt+1,st.top().second);
                st.pop();
            }
            ans=max(ans,cnt);
            st.push({nums[i],cnt});
        }
        return ans;
    }
};

Steps to Make Array Non-decreasing LeetCode Solution in Java

   public int totalSteps(int[] nums) {
        int ans = 0;
        Stack<int[]> stack = new Stack<int[]>();
        for(int i = nums.length-1; i >= 0; i--) {
            if(stack.isEmpty() || stack.peek()[0] >= nums[i]) {
                stack.push(new int[]{nums[i], 0});
            }else{
                int count = 0;
                while(!stack.isEmpty() && stack.peek()[0] < nums[i]) {
                    count++;
                    int[] item = stack.pop();
                    if(count < item[1]) count += (item[1] - count);
                }
                stack.push(new int[]{nums[i], count});
                ans = Math.max(ans, count);
            }
        }
        
        return ans;
    }
Steps to Make Array Non-decreasing LeetCode Solution Review:

In our experience, we suggest you solve this Steps to Make Array Non-decreasing LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Steps to Make Array Non-decreasing LeetCode Solution

Find on LeetCode

Conclusion:

I hope this Steps to Make Array Non-decreasing LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.