LeetCode 题解工作台

统计包含每个点的矩形数目

给你一个二维整数数组 rectangles ,其中 rectangles[i] = [l i , h i ] 表示第 i 个矩形长为 l i 高为 h i 。给你一个二维整数数组 points ,其中 points[j] = [x j , y j ] 是坐标为 (x j , y j ) 的一个点。 …

category

5

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·哈希·扫描

bolt

答案摘要

class Solution: def countRectangles(

Interview AiBox logo

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

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个二维整数数组 rectangles ,其中 rectangles[i] = [li, hi] 表示第 i 个矩形长为 li 高为 hi 。给你一个二维整数数组 points ,其中 points[j] = [xj, yj] 是坐标为 (xj, yj) 的一个点。

第 i 个矩形的 左下角 在 (0, 0) 处,右上角 在 (li, hi) 。

请你返回一个整数数组 count ,长度为 points.length,其中 count[j]包含 j 个点的矩形数目。

如果 0 <= xj <= li 且 0 <= yj <= hi ,那么我们说第 i 个矩形包含第 j 个点。如果一个点刚好在矩形的 边上 ,这个点也被视为被矩形包含。

 

示例 1:

输入:rectangles = [[1,2],[2,3],[2,5]], points = [[2,1],[1,4]]
输出:[2,1]
解释:
第一个矩形不包含任何点。
第二个矩形只包含一个点 (2, 1) 。
第三个矩形包含点 (2, 1) 和 (1, 4) 。
包含点 (2, 1) 的矩形数目为 2 。
包含点 (1, 4) 的矩形数目为 1 。
所以,我们返回 [2, 1] 。

示例 2:

输入:rectangles = [[1,1],[2,2],[3,3]], points = [[1,3],[1,1]]
输出:[1,3]
解释:
第一个矩形只包含点 (1, 1) 。
第二个矩形只包含点 (1, 1) 。
第三个矩形包含点 (1, 3) 和 (1, 1) 。
包含点 (1, 3) 的矩形数目为 1 。
包含点 (1, 1) 的矩形数目为 3 。
所以,我们返回 [1, 3] 。

 

提示:

  • 1 <= rectangles.length, points.length <= 5 * 104
  • rectangles[i].length == points[j].length == 2
  • 1 <= li, xj <= 109
  • 1 <= hi, yj <= 100
  • 所有 rectangles 互不相同 。
  • 所有 points 互不相同 。
lightbulb

解题思路

方法一:排序 + 二分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution:
    def countRectangles(
        self, rectangles: List[List[int]], points: List[List[int]]
    ) -> List[int]:
        d = defaultdict(list)
        for x, y in rectangles:
            d[y].append(x)
        for y in d.keys():
            d[y].sort()
        ans = []
        for x, y in points:
            cnt = 0
            for h in range(y, 101):
                xs = d[h]
                cnt += len(xs) - bisect_left(xs, x)
            ans.append(cnt)
        return ans
speed

复杂度分析

指标
时间complexity depends on the approach chosen. The brute force solution involves checking every point against all rectangles, leading to O(N*M) complexity, where N is the number of rectangles and M is the number of points. Optimizations such as binary search or pre-computation can reduce this. Space complexity also depends on the approach, with storage for rectangles and points taking O(N + M) space in the worst case.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Candidate's ability to optimize brute force methods for large inputs.

  • question_mark

    Awareness of sorting and binary search optimization techniques.

  • question_mark

    Understanding of hash table usage for efficient lookup and pre-computation.

warning

常见陷阱

外企场景
  • error

    Incorrectly assuming all rectangles are relevant for each point, leading to excessive checks.

  • error

    Failure to optimize searching for rectangles by overlooking sorting or binary search options.

  • error

    Not considering space and time trade-offs when scaling to the upper constraint limits.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    What if rectangles or points had larger or smaller values?

  • arrow_right_alt

    How would the problem change if rectangles were not guaranteed to have unique dimensions?

  • arrow_right_alt

    How can we modify this approach to handle a dynamically changing set of rectangles or points?

help

常见问题

外企场景

统计包含每个点的矩形数目题解:数组·哈希·扫描 | LeetCode #2250 中等