Skip to content

1827. Minimum Operations to Make the Array Increasing 👍

  • Time: $O(n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
 public:
  int minOperations(vector<int>& nums) {
    int ans = 0;
    int last = 0;

    for (const int num : nums) {
      ans += max(0, last - num + 1);
      last = max(num, last + 1);
    }

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
  public int minOperations(int[] nums) {
    int ans = 0;
    int last = 0;

    for (final int num : nums) {
      ans += Math.max(0, last - num + 1);
      last = Math.max(num, last + 1);
    }

    return ans;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution:
  def minOperations(self, nums: list[int]) -> int:
    ans = 0
    last = 0

    for num in nums:
      ans += max(0, last - num + 1)
      last = max(num, last + 1)

    return ans