LeetCode 题解工作台

检查是否区域内所有整数都被覆盖

给你一个二维整数数组 ranges 和两个整数 left 和 right 。每个 ranges[i] = [start i , end i ] 表示一个从 start i 到 end i 的 闭区间 。 如果闭区间 [left, right] 内每个整数都被 ranges 中 至少一个 区间覆盖,那…

category

3

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

简单 · 数组·哈希·扫描

bolt

答案摘要

我们可以使用差分数组的思想,创建一个长度为 的差分数组 。 接下来,我们遍历数组 ,对于每个区间 $[l, r]$,我们令 自增 ,而 $\textit{diff}[r + 1]$ 自减 。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个二维整数数组 ranges 和两个整数 left 和 right 。每个 ranges[i] = [starti, endi] 表示一个从 starti 到 endi 的 闭区间 。

如果闭区间 [left, right] 内每个整数都被 ranges 中 至少一个 区间覆盖,那么请你返回 true ,否则返回 false 。

已知区间 ranges[i] = [starti, endi] ,如果整数 x 满足 starti <= x <= endi ,那么我们称整数x 被覆盖了。

 

示例 1:

输入:ranges = [[1,2],[3,4],[5,6]], left = 2, right = 5
输出:true
解释:2 到 5 的每个整数都被覆盖了:
- 2 被第一个区间覆盖。
- 3 和 4 被第二个区间覆盖。
- 5 被第三个区间覆盖。

示例 2:

输入:ranges = [[1,10],[10,20]], left = 21, right = 21
输出:false
解释:21 没有被任何一个区间覆盖。

 

提示:

  • 1 <= ranges.length <= 50
  • 1 <= starti <= endi <= 50
  • 1 <= left <= right <= 50
lightbulb

解题思路

方法一:差分数组

我们可以使用差分数组的思想,创建一个长度为 5252 的差分数组 diff\textit{diff}

接下来,我们遍历数组 ranges\textit{ranges},对于每个区间 [l,r][l, r],我们令 diff[l]\textit{diff}[l] 自增 11,而 diff[r+1]\textit{diff}[r + 1] 自减 11

接着,我们遍历差分数组 diff\textit{diff},维护一个前缀和 ss,对于每个位置 ii,我们令 ss 自增 diff[i]\textit{diff}[i],如果 s0s \le 0leftirightleft \le i \le right,则说明区间 [left,right][left, right] 中有一个整数 ii 没有被覆盖,返回 false\textit{false}

如果遍历完差分数组 diff\textit{diff} 后都没有返回 false\textit{false},则说明区间 [left,right][left, right] 中的每个整数都被 ranges\textit{ranges} 中至少一个区间覆盖,返回 true\textit{true}

时间复杂度 O(n+M)O(n + M),空间复杂度 O(M)O(M)。其中 nn 是数组 ranges\textit{ranges} 的长度,而 MM 是区间的最大值,本题中 M50M \le 50

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:
        diff = [0] * 52
        for l, r in ranges:
            diff[l] += 1
            diff[r + 1] -= 1
        s = 0
        for i, x in enumerate(diff):
            s += x
            if s <= 0 and left <= i <= right:
                return False
        return True
speed

复杂度分析

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

面试官常问的追问

外企场景
  • question_mark

    The candidate shows understanding of array iteration techniques.

  • question_mark

    The candidate demonstrates awareness of optimizing brute-force solutions.

  • question_mark

    The candidate considers the use of advanced data structures for improving performance.

warning

常见陷阱

外企场景
  • error

    Overcomplicating the solution with unnecessary data structures for small inputs.

  • error

    Forgetting to check coverage for every integer between left and right.

  • error

    Confusing inclusive and exclusive range bounds when verifying coverage.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    What if the intervals can overlap?

  • arrow_right_alt

    How would you approach the problem if the range size was very large?

  • arrow_right_alt

    Can the solution be optimized if ranges are sorted?

help

常见问题

外企场景

检查是否区域内所有整数都被覆盖题解:数组·哈希·扫描 | LeetCode #1893 简单