EasyNo. 1827

Minimum Operations to Make the Array Increasing — LeetCode 1827 Python Solution

Pattern
Greedy
Topics
Greedy, Array
Reading time
2 min

The problem

You are given an integer array nums (0-indexed). In one operation, you can choose an element of the array and increment it by 1.

Example

Input
nums = [1,1,1]
Output
3
Explanation
You can do the following operations:

Python solution

PythonSolution
class Solution:
    def minOperations(self, nums: List[int]) -> int:
        ans = mx = 0
        for v in nums:
            ans += max(0, mx + 1 - v)
            mx = max(mx + 1, v)
        return ans

Complexity

Time
O(n)where n is the length of the array `nums`
Space · auxiliary
O(1)

Pattern: Greedy

Take the locally best option every time — when you can prove that never costs you later. LeetCode 1827. Minimum Operations to Make the Array Increasing is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Greedy.

The greedy guide has the Python template for the pattern and the 346 LeetCode problems that use it.

Related problems

Related problems
No.ProblemDifficulty
1833Maximum Ice Cream BarsMedium
0011Container With Most WaterMedium
0134Gas StationMedium
0135CandyHard
0179Largest NumberMedium
0324Wiggle Sort IIMedium

Frequently asked questions

How hard is LeetCode 1827. Minimum Operations to Make the Array Increasing?

LeetCode 1827. Minimum Operations to Make the Array Increasing is rated Easy on LeetCode.

What is the time complexity of LeetCode 1827. Minimum Operations to Make the Array Increasing?

The Python solution on this page runs in O(n), where n is the length of the array `nums`.

What is the space complexity of LeetCode 1827. Minimum Operations to Make the Array Increasing?

The Python solution on this page uses O(1) auxiliary space.

What topics does LeetCode 1827. Minimum Operations to Make the Array Increasing cover?

LeetCode 1827. Minimum Operations to Make the Array Increasing is tagged Greedy and Array on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.