Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/2289.html

2289. Steps to Make Array Non-decreasing

  • Difficulty: Medium.
  • Related Topics: Array, Linked List, Stack, Monotonic Stack.
  • Similar Questions: Remove One Element to Make the Array Strictly Increasing.

Problem

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

Solution (Java, C++, Python)

  • class Solution {
        public int totalSteps(int[] nums) {
            int max = 0;
            int[] pos = new int[nums.length + 1];
            int[] steps = new int[nums.length + 1];
            int top = -1;
            for (int i = 0; i <= nums.length; i++) {
                int val = i == nums.length ? Integer.MAX_VALUE : nums[i];
                while (top >= 0 && nums[pos[top]] <= val) {
                    if (top == 0) {
                        max = Math.max(max, steps[pos[top--]]);
                    } else {
                        steps[pos[--top]] = Math.max(steps[pos[top]] + 1, steps[pos[top + 1]]);
                    }
                }
                pos[++top] = i;
            }
            return max;
        }
    }
    
  • Todo
    
  • class Solution:
        def totalSteps(self, nums: List[int]) -> int:
            stk = []
            ans, n = 0, len(nums)
            dp = [0] * n
            for i in range(n - 1, -1, -1):
                while stk and nums[i] > nums[stk[-1]]:
                    dp[i] = max(dp[i] + 1, dp[stk.pop()])
                stk.append(i)
            return max(dp)
    
    ############
    
    # 2289. Steps to Make Array Non-decreasing
    # https://leetcode.com/problems/steps-to-make-array-non-decreasing
    
    from sortedcontainers import SortedList
    
    class Solution:
        def totalSteps(self, nums: List[int]) -> int:
            nums = [float('-inf')] + nums + [float('inf')]
            res = 0
            
            sl = SortedList([(i, x) for i, x in enumerate(nums)])
            p = set()
            
            for i, (a, b) in enumerate(zip(nums, nums[1:])):
                if a > b:
                    p.add((i + 1, b))
                    
            while p:
                newP = set()
                res += 1
                
                for j, b in p:
                    index = sl.bisect_left((j, b))
                    i, a = sl[index - 1]
                    k, c = sl[index + 1]
                    del sl[index]
                    
                    if a > c and (k, c) not in p:
                        newP.add((k, c))
                
                p = newP
            
            return res
    
    

Explain:

nope.

Complexity:

  • Time complexity : O(n).
  • Space complexity : O(n).

All Problems

All Solutions