2289. Steps to Make Array Non-decreasing

Обновлено: 2024-03-12
1 мин
[]

Содержание

LeetCode problem 2289

class Solution:
    def totalSteps(self, nums: List[int]) -> int:
        stk = []
        res, 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)