681. Steps to Make Array Non-decreasing

HardStackMonotonic StackArray

In one step, every element strictly greater than the element immediately to its left is removed simultaneously. Repeat until the array is non-decreasing. Given nums, return the number of steps performed. The input is JSON {nums}.

Input: JSON {nums}.

Output: Integer — the number of steps.

Examples

Example 1
Input: {"nums":[5,3,4,4,7,3,6,11,8,5,11]}
Output: 3
Explanation: Three rounds of removals.
Example 2
Input: {"nums":[4,5,7,7,13]}
Output: 0
Explanation: Already non-decreasing.
Example 3
Input: {"nums":[7]}
Output: 0
Explanation: Single element.

Constraints

Asked by

Meta
Solve this problem in the editor →