minimum operations to make the array increasing

https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/

Input: nums = [1,1,1]
Output: 3
Input: nums = [1,5,2,4,1]
Output: 14
Input: nums = [8]
Output: 0

Initial wrong attempt:

class Solution:
    def minOperations(self, nums: List[int]) -> int:
        n = len(nums)
        s = 0

        if n == 1: return 0

        for i in range(1, n):
            if nums[i] > nums[i-1]:
                continue
            else:
                s = i
                break
        
        ans = 0
        
        if s == 0:
            return ans
        else:
            prev = nums[s-1] + 1

        for i in range(s, n):
            ans += prev - nums[i]
            prev += 1
        
        return ans

minimum replacements to sort the array

Misc Problem Info

Difficulty: Easy
Tags: adp related to greedy


Backlinks