Steps to Make Array Non-decreasing

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
Inputnums = [5,3,4,4,7,3,6,11,8,5,11]
Output3
After three removal steps, the array becomes [5, 7, 11, 11], which is non-decreasing.
Example 2
Inputnums = [4,5,7,7,13]
Output0
nums is already a non-decreasing array, so no steps are needed.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^9

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate