题目来源于力扣——1827. 最少操作使数组递增 - 力扣(LeetCode) (leetcode-cn.com)
给你一个整数数组 nums (下标从 0 开始)。每一次操作中,你可以选择数组中一个元素,并将它增加 1 。
比方说,如果 nums = [1,2,3] ,你可以选择增加 nums[1] 得到 nums = [1,3,3] 。
请你返回使 nums 严格递增 的 最少 操作次数。我们称数组 nums 是 严格递增的 ,当它满足对于所有的 0 <= i < nums.length - 1 都有 nums[i] < nums[i+1] 。一个长度为 1 的数组是严格递增的一种特殊情况。
示例 1:
输入:nums = [1,1,1]
输出:3
解释:你可以进行如下操作:
1) 增加 nums[2] ,数组变为 [1,1,2] 。
2) 增加 nums[1] ,数组变为 [1,2,2] 。
3) 增加 nums[2] ,数组变为 [1,2,3] 。
示例 2:输入:nums = [1,5,2,4,1]
输出:14
示例 3:输入:nums = [8]
输出:0
int minOperations(int* nums, int numsSize){
if(numsSize == 1)
{
return 0;
}
int i = 0;
int j = 0;
int count = 0;
for(i=0,j=1;i<numsSize,j<numsSize;i++,j++)
{
if(nums[i]>=nums[j])
{
while(nums[i]>=nums[j])
{
nums[j]++;
count++;
}
}
}
return count;
}