LeetCode 题解工作台

零数组变换 I

给定一个长度为 n 的整数数组 nums 和一个二维数组 queries ,其中 queries[i] = [l i , r i ] 。 对于每个查询 queries[i] : 在 nums 的下标范围 [l i , r i ] 内选择一个下标 子集 。 将选中的每个下标对应的元素值减 1。 零数组…

category

2

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 前缀和

bolt

答案摘要

我们可以使用差分数组来解决这个问题。 定义一个长度为 $n + 1$ 的数组 ,初始值全部为 。对于每个查询 $[l, r]$,我们将 加 ,将 $d[r + 1]$ 减 。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 前缀和 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给定一个长度为 n 的整数数组 nums 和一个二维数组 queries,其中 queries[i] = [li, ri]

对于每个查询 queries[i]

  • 在 nums 的下标范围 [li, ri] 内选择一个下标 子集
  • 将选中的每个下标对应的元素值减 1。

零数组 是指所有元素都等于 0 的数组。

如果在按顺序处理所有查询后,可以将 nums 转换为 零数组 ,则返回 true,否则返回 false

 

示例 1:

输入: nums = [1,0,1], queries = [[0,2]]

输出: true

解释:

  • 对于 i = 0:
    • 选择下标子集 [0, 2] 并将这些下标处的值减 1。
    • 数组将变为 [0, 0, 0],这是一个零数组。

示例 2:

输入: nums = [4,3,2,1], queries = [[1,3],[0,2]]

输出: false

解释:

  • 对于 i = 0: 
    • 选择下标子集 [1, 2, 3] 并将这些下标处的值减 1。
    • 数组将变为 [4, 2, 1, 0]
  • 对于 i = 1:
    • 选择下标子集 [0, 1, 2] 并将这些下标处的值减 1。
    • 数组将变为 [3, 1, 0, 0],这不是一个零数组。

 

提示:

  • 1 <= nums.length <= 105
  • 0 <= nums[i] <= 105
  • 1 <= queries.length <= 105
  • queries[i].length == 2
  • 0 <= li <= ri < nums.length
lightbulb

解题思路

方法一:差分数组

我们可以使用差分数组来解决这个问题。

定义一个长度为 n+1n + 1 的数组 dd,初始值全部为 00。对于每个查询 [l,r][l, r],我们将 d[l]d[l]11,将 d[r+1]d[r + 1]11

然后我们遍历数组 dd[0,n1][0, n - 1] 范围内的每个元素,累加前缀和 ss,如果 nums[i]>s\textit{nums}[i] > s,说明 nums\textit{nums} 不能转换为零数组,返回 false\textit{false}

遍历结束后,返回 true\textit{true}

时间复杂度 O(n+m)O(n + m),空间复杂度 O(n)O(n)。其中 nnmm 分别为数组 nums\textit{nums}queries\textit{queries} 的长度。

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
    def isZeroArray(self, nums: List[int], queries: List[List[int]]) -> bool:
        d = [0] * (len(nums) + 1)
        for l, r in queries:
            d[l] += 1
            d[r + 1] -= 1
        s = 0
        for x, y in zip(nums, d):
            s += y
            if x > s:
                return False
        return True
speed

复杂度分析

指标
时间complexity is O(n + m) where n is the length of nums and m is the number of queries, because each query is processed once and prefix sums are computed in a single pass. Space complexity is O(n) for the difference array.
空间O(n)
psychology

面试官常问的追问

外企场景
  • question_mark

    Check whether you are correctly using a difference array instead of modifying the original array per query.

  • question_mark

    Make sure your prefix sum computation correctly accumulates all range operations.

  • question_mark

    Consider edge cases where queries overlap or cover entire array sections.

warning

常见陷阱

外企场景
  • error

    Updating the original array directly for each query leads to TLE for large n or m.

  • error

    Incorrect handling of diff[r+1] can miscalculate net effects at array boundaries.

  • error

    Forgetting to validate that all elements are zero after prefix sum accumulation.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Allowing queries that increment or decrement by different values, not just ±1.

  • arrow_right_alt

    Checking for partial zero arrays where only a subset must be zero.

  • arrow_right_alt

    Transforming using non-contiguous index sets instead of continuous ranges.

help

常见问题

外企场景

零数组变换 I题解:前缀和 | LeetCode #3355 中等