LeetCode 题解工作台

使数组变美的最小增量运算数

给你一个下标从 0 开始、长度为 n 的整数数组 nums ,和一个整数 k 。 你可以执行下述 递增 运算 任意 次(可以是 0 次): 从范围 [0, n - 1] 中选择一个下标 i ,并将 nums[i] 的值加 1 。 如果数组中任何长度 大于或等于 3 的子数组,其 最大 元素都大于或等…

category

2

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

中等 · 状态·转移·动态规划

bolt

答案摘要

我们定义 , , 表示前 项中,分别以最后三项作为子数组的最大值所需要的最小增量运算数,初始时 $f = 0$, $g = 0$, $h = 0$。 接下来,我们遍历数组 ,对于每个 ,我们需要更新 , , 的值,使其满足题目要求,即:

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个下标从 0 开始、长度为 n 的整数数组 nums ,和一个整数 k

你可以执行下述 递增 运算 任意 次(可以是 0 次):

  • 从范围 [0, n - 1] 中选择一个下标 i ,并将 nums[i] 的值加 1

如果数组中任何长度 大于或等于 3 的子数组,其 最大 元素都大于或等于 k ,则认为数组是一个 美丽数组

以整数形式返回使数组变为 美丽数组 需要执行的 最小 递增运算数。

子数组是数组中的一个连续 非空 元素序列。

 

示例 1:

输入:nums = [2,3,0,0,2], k = 4
输出:3
解释:可以执行下述递增运算,使 nums 变为美丽数组:
选择下标 i = 1 ,并且将 nums[1] 的值加 1 -> [2,4,0,0,2] 。
选择下标 i = 4 ,并且将 nums[4] 的值加 1 -> [2,4,0,0,3] 。
选择下标 i = 4 ,并且将 nums[4] 的值加 1 -> [2,4,0,0,4] 。
长度大于或等于 3 的子数组为 [2,4,0], [4,0,0], [0,0,4], [2,4,0,0], [4,0,0,4], [2,4,0,0,4] 。
在所有子数组中,最大元素都等于 k = 4 ,所以 nums 现在是美丽数组。
可以证明无法用少于 3 次递增运算使 nums 变为美丽数组。
因此,答案为 3 。

示例 2:

输入:nums = [0,1,3,3], k = 5
输出:2
解释:可以执行下述递增运算,使 nums 变为美丽数组:
选择下标 i = 2 ,并且将 nums[2] 的值加 1 -> [0,1,4,3] 。
选择下标 i = 2 ,并且将 nums[2] 的值加 1 -> [0,1,5,3] 。
长度大于或等于 3 的子数组为 [0,1,5]、[1,5,3]、[0,1,5,3] 。
在所有子数组中,最大元素都等于 k = 5 ,所以 nums 现在是美丽数组。
可以证明无法用少于 2 次递增运算使 nums 变为美丽数组。 
因此,答案为 2 。

示例 3:

输入:nums = [1,1,2], k = 1
输出:0
解释:在这个示例中,只有一个长度大于或等于 3 的子数组 [1,1,2] 。
其最大元素 2 已经大于 k = 1 ,所以无需执行任何增量运算。
因此,答案为 0 。

 

提示:

  • 3 <= n == nums.length <= 105
  • 0 <= nums[i] <= 109
  • 0 <= k <= 109
lightbulb

解题思路

方法一:动态规划

我们定义 ff, gg, hh 表示前 ii 项中,分别以最后三项作为子数组的最大值所需要的最小增量运算数,初始时 f=0f = 0, g=0g = 0, h=0h = 0

接下来,我们遍历数组 numsnums,对于每个 xx,我们需要更新 ff, gg, hh 的值,使其满足题目要求,即:

f=gg=hh=min(f,g,h)+max(kx,0)\begin{aligned} f' &= g \\ g' &= h \\ h' &= \min(f, g, h) + \max(k - x, 0) \end{aligned}

最后,我们只需要返回 ff, gg, hh 中的最小值即可。

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

1
2
3
4
5
6
7
class Solution:
    def minIncrementOperations(self, nums: List[int], k: int) -> int:
        f = g = h = 0
        for x in nums:
            f, g, h = g, h, min(f, g, h) + max(k - x, 0)
        return min(f, g, h)
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Look for understanding of dynamic programming and how state transitions can be used to minimize operations.

  • question_mark

    Evaluate the candidate's ability to optimize with a greedy or sliding window approach to handle large inputs efficiently.

  • question_mark

    Check if the candidate can explain the trade-offs between different approaches based on the constraints.

warning

常见陷阱

外企场景
  • error

    Failing to correctly track the minimum number of operations needed for each possible state transition.

  • error

    Overlooking the fact that an optimal solution requires ensuring the maximum value condition for all subarrays of size 3 or more.

  • error

    Not considering edge cases where the array is already beautiful or requires no operations.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Instead of tracking the minimum operations, track the number of subarrays that meet the condition.

  • arrow_right_alt

    Modify the problem to work with a sliding window of size 4 or more instead of 3 or more.

  • arrow_right_alt

    Introduce constraints where the maximum value of k is dynamically adjusted during the process.

help

常见问题

外企场景

使数组变美的最小增量运算数题解:状态·转移·动态规划 | LeetCode #2919 中等