Minimum Operations to Make the Array Increasing

We traverse through the array and whenever we find that the current number is less than or equal to the previous one, we increment it to make it strictly greater than the previous one. The minimum number of operations would be the difference between the incremented number and the original number.

Python solution:

1
2
3
4
5
6
7
8
9
class Solution:
    def minOperations(self, nums: List[int]) -> int:
        operations = 0
        for i in range(1, len(nums)):
            if nums[i] <= nums[i - 1]:
                difference = nums[i - 1] - nums[i] + 1
                nums[i] += difference
                operations += difference
        return operations

We initialize the operations counter to 0. Then, for each number in the array starting from the second element, we check if it’s less than or equal to the previous number. If it is, we calculate the difference and increment the current number and the operations counter by the difference.

This approach ensures that we make the least number of increments, because we increment each number to just 1 more than the previous number.

The time complexity is O(n), where n is the size of the input array, since we go through the array once. The space complexity is O(1), because we only use a constant amount of extra space to store the operations counter and the difference.