leet-code/src/main/java/leetcode/editor/cn/MinimumOperationsToMakeTheArrayIncreasing.java

73 lines
1.8 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//给你一个整数数组 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
//
//
//
//
// 提示:
//
//
// 1 <= nums.length <= 5000
// 1 <= nums[i] <= 104
//
// Related Topics 贪心算法 数组
// 👍 7 👎 0
package leetcode.editor.cn;
//1827:最少操作使数组递增
public class MinimumOperationsToMakeTheArrayIncreasing{
public static void main(String[] args) {
//测试代码
Solution solution = new MinimumOperationsToMakeTheArrayIncreasing().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int minOperations(int[] nums) {
int left = nums[0] - 1;
int sum = 0;
for (int num : nums) {
if (num <= left) {
sum += left - num + 1;
left++;
}else{
left = num;
}
}
return sum;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}