2374. Steps to Make Array Non-decreasing
My accepted Python solution to LeetCode problem 2374, Steps to Make Array Non-decreasing, running in 191ms.
- Difficulty: Medium
- Python
- Runtime 191ms
- Memory 33.2MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 191ms, memory 33.2MB, accepted 2026-01-02.
class Solution:
def totalSteps(self, nums: List[int]) -> int:
n = len(nums)
stack = [] # (value, steps to remove this element)
result = 0
for i in range(n - 1, -1, -1):
steps = 0
while stack and nums[i] > stack[-1][0]:
steps = max(steps + 1, stack[-1][1])
stack.pop()
stack.append((nums[i], steps))
result = max(result, steps)
return result