LeetCode 题解工作台

最少操作使数组递增

给你一个整数数组 nums ( 下标从 0 开始 )。每一次操作中,你可以选择数组中一个元素,并将它增加 1 。 比方说,如果 nums = [1,2,3] ,你可以选择增加 nums[1] 得到 nums = [1, 3 ,3] 。 请你返回使 nums 严格递增 的 最少 操作次数。 我们称数组…

category

2

题型

code_blocks

8

代码语言

hub

3

相关题

当前训练重点

简单 · 贪心·invariant

bolt

答案摘要

我们用变量 记录当前严格递增数组的最大值,初始时 $mx = 0$。 从左到右遍历数组 `nums`,对于当前遍历到的元素 ,如果 $v \lt mx + 1$,那么我们需要将其增加到 $mx + 1$,这样才能保证数组严格递增。因此,我们此次需要进行的操作次数为 $max(0, mx + 1 - v)$,累加到答案中,然后更新 $mx=max(mx + 1, v)$。继续遍历下一个元素,直到遍…

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 贪心·invariant 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个整数数组 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
lightbulb

解题思路

方法一:一次遍历

我们用变量 mxmx 记录当前严格递增数组的最大值,初始时 mx=0mx = 0

从左到右遍历数组 nums,对于当前遍历到的元素 vv,如果 v<mx+1v \lt mx + 1,那么我们需要将其增加到 mx+1mx + 1,这样才能保证数组严格递增。因此,我们此次需要进行的操作次数为 max(0,mx+1v)max(0, mx + 1 - v),累加到答案中,然后更新 mx=max(mx+1,v)mx=max(mx + 1, v)。继续遍历下一个元素,直到遍历完整个数组。

时间复杂度 O(n)O(n),其中 nn 为数组 nums 的长度。空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
8
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
speed

复杂度分析

指标
时间complexity is O(n) since each element is visited once. Space complexity is O(1) extra if modifying in-place, otherwise O(n) if using a separate array to track increments.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Notice the array must be strictly increasing, hinting at element-wise comparison.

  • question_mark

    Greedy approach is expected, incrementing only when necessary to maintain the invariant.

  • question_mark

    Tracking total operations while scanning sequentially shows awareness of minimal adjustments.

warning

常见陷阱

外企场景
  • error

    Failing to compare nums[i+1] with the already updated nums[i], causing undercounted operations.

  • error

    Using nested loops instead of a single pass, resulting in unnecessary time complexity.

  • error

    Ignoring the edge case where the array has only one element, which is trivially strictly increasing.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Compute minimum decrements to make the array strictly decreasing using a similar greedy pattern.

  • arrow_right_alt

    Find minimum operations if allowed to increment or decrement each element by 1, balancing adjustments.

  • arrow_right_alt

    Apply the approach to 2D arrays, ensuring each row or column is strictly increasing.

help

常见问题

外企场景

最少操作使数组递增题解:贪心·invariant | LeetCode #1827 简单