Minimum Operations to Make the Array Increasing — LeetCode 1827 Python Solution
EasyGreedyArray
- Problem
- #1827
- Pattern
- Greedy
- Reading time
- 2 min
- Source
- leetcode.com
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
Python
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 ansComplexity
| Measure | Complexity |
|---|---|
| Time | O(n), where n is the length of the array `nums` |
| Space | O(1) auxiliary |
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
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.