LeetCode 题解工作台

形成目标数组的子数组最少增加次数

给你一个整数数组 target 和一个数组 initial , initial 数组与 target 数组有同样的大小,且一开始全部为 0 。 一次操作中,你可以从 initial 数组中选择 任何 子数组,并将每个值加 1 。 返回从 initial 数组构造 target 数组的最少操作次数。 …

category

5

题型

code_blocks

7

代码语言

hub

3

相关题

当前训练重点

困难 · 状态·转移·动态规划

bolt

答案摘要

我们定义 表示得到 的最少操作次数,初始时 $f[0] = target[0]$。 对于 ,如果 $target[i] \leq target[i-1]$,则 $f[i] = f[i-1]$;否则 $f[i] = f[i-1] + target[i] - target[i-1]$。

Interview AiBox logo

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

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个整数数组 target 和一个数组 initial ,initial 数组与 target  数组有同样的大小,且一开始全部为 0 。

一次操作中,你可以从 initial 数组中选择 任何 子数组,并将每个值加 1

返回从 initial 数组构造 target 数组的最少操作次数。

答案保证在 32 位整数以内。

 

示例 1:

输入:target = [1,2,3,2,1]
输出:3
解释:我们需要至少 3 次操作从 intial 数组得到 target 数组。
[0,0,0,0,0] 将下标为 0 到 4 的元素(包含二者)加 1 。
[1,1,1,1,1] 将下标为 1 到 3 的元素(包含二者)加 1 。
[1,2,2,2,1] 将下标为 2 的元素增加 1 。
[1,2,3,2,1] 得到了目标数组。

示例 2:

输入:target = [3,1,1,2]
输出:4
解释:(initial)[0,0,0,0] -> [1,1,1,1] -> [1,1,1,2] -> [2,1,1,2] -> [3,1,1,2] (target) 。

示例 3:

输入:target = [3,1,5,4,2]
输出:7
解释:(initial)[0,0,0,0,0] -> [1,1,1,1,1] -> [2,1,1,1,1] -> [3,1,1,1,1] 
                                  -> [3,1,2,2,2] -> [3,1,3,3,2] -> [3,1,4,4,2] -> [3,1,5,4,2] (target)。

示例 4:

输入:target = [1,1,1,1]
输出:1

 

提示:

  • 1 <= target.length <= 105
  • 1 <= target[i] <= 105
  • 输入保证答案在 32 位整数范围内。
lightbulb

解题思路

方法一:动态规划

我们定义 f[i]f[i] 表示得到 target[0,..i]target[0,..i] 的最少操作次数,初始时 f[0]=target[0]f[0] = target[0]

对于 target[i]target[i],如果 target[i]target[i1]target[i] \leq target[i-1],则 f[i]=f[i1]f[i] = f[i-1];否则 f[i]=f[i1]+target[i]target[i1]f[i] = f[i-1] + target[i] - target[i-1]

最终答案即为 f[n1]f[n-1]

我们注意到 f[i]f[i] 只与 f[i1]f[i-1] 有关,因此可以只用一个变量来维护操作次数。

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

相似题目:

1
2
3
4
class Solution:
    def minNumberOperations(self, target: List[int]) -> int:
        return target[0] + sum(max(0, b - a) for a, b in pairwise(target))
speed

复杂度分析

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

面试官常问的追问

外企场景
  • question_mark

    Does the candidate effectively use state transition dynamic programming for the problem?

  • question_mark

    Is the candidate comfortable applying greedy strategies for range-based optimization?

  • question_mark

    Does the candidate demonstrate proficiency in managing subarray operations with a monotonic stack?

warning

常见陷阱

外企场景
  • error

    Incorrectly handling the subarray increments could lead to more operations than necessary.

  • error

    Failing to optimize range increments might result in a solution that exceeds the required number of operations.

  • error

    Overcomplicating the solution with unnecessary nested loops or complex data structures can increase time complexity.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Modifying the problem to work with a non-zero initial array, requiring adjustments to the dynamic programming approach.

  • arrow_right_alt

    Allowing multiple increments on the same subarray, testing the candidate's ability to handle more complex operations.

  • arrow_right_alt

    Changing the target array values to be random or non-sequential, requiring more adaptive range-based strategies.

help

常见问题

外企场景

形成目标数组的子数组最少增加次数题解:状态·转移·动态规划 | LeetCode #1526 困难