2366. Minimum Replacements to Sort the Array

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2366

class Solution:
    def minimumReplacement(self, nums: List[int]) -> int:
        res = 0
        n = len(nums)
        mx = nums[-1]
        for i in range(n - 2, -1, -1):
            if nums[i] <= mx:
                mx = nums[i]
                continue
            k = (nums[i] + mx - 1) // mx
            res += k - 1
            mx = nums[i] // k
        return res