LeetCode 题解工作台
求交集区域内的最大正方形面积
在二维平面上存在 n 个矩形。给你两个下标从 0 开始的二维整数数组 bottomLeft 和 topRight ,两个数组的大小都是 n x 2 ,其中 bottomLeft[i] 和 topRight[i] 分别代表第 i 个矩形的 左下角 和 右上角 坐标。 我们定义 向右 的方向为 x 轴正…
3
题型
6
代码语言
3
相关题
当前训练重点
中等 · 数组·数学
答案摘要
我们可以枚举两个矩形,其中矩形 的左下角和右上角坐标分别为 $(x_1, y_1)$ 和 $(x_2, y_2)$,矩形 的左下角和右上角坐标分别为 $(x_3, y_3)$ 和 $(x_4, y_4)$。 如果矩形 和矩形 有交集,那么交集的坐标分别为:
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 数组·数学 题型思路
题目描述
在二维平面上存在 n 个矩形。给你两个下标从 0 开始的二维整数数组 bottomLeft 和 topRight,两个数组的大小都是 n x 2 ,其中 bottomLeft[i] 和 topRight[i] 分别代表第 i 个矩形的 左下角 和 右上角 坐标。
我们定义 向右 的方向为 x 轴正半轴(x 坐标增加),向左 的方向为 x 轴负半轴(x 坐标减少)。同样地,定义 向上 的方向为 y 轴正半轴(y 坐标增加),向下 的方向为 y 轴负半轴(y 坐标减少)。
你可以选择一个区域,该区域由两个矩形的 交集 形成。你需要找出能够放入该区域 内 的 最大 正方形面积,并选择最优解。
返回能够放入交集区域的正方形的 最大 可能面积,如果矩形之间不存在任何交集区域,则返回 0。
示例 1:
输入:bottomLeft = [[1,1],[2,2],[3,1]], topRight = [[3,3],[4,4],[6,6]] 输出:1 解释:边长为 1 的正方形可以放入矩形 0 和矩形 1 的交集区域,或矩形 1 和矩形 2 的交集区域。因此最大面积是边长 * 边长,即 1 * 1 = 1。 可以证明,边长更大的正方形无法放入任何交集区域。
示例 2:
输入:bottomLeft = [[1,1],[2,2],[1,2]], topRight = [[3,3],[4,4],[3,4]] 输出:1 解释:边长为 1 的正方形可以放入矩形 0 和矩形 1,矩形 1 和矩形 2,或所有三个矩形的交集区域。因此最大面积是边长 * 边长,即 1 * 1 = 1。 可以证明,边长更大的正方形无法放入任何交集区域。 请注意,区域可以由多于两个矩形的交集构成。
示例 3:
输入:bottomLeft = [[1,1],[3,3],[3,1]], topRight = [[2,2],[4,4],[4,2]] 输出:0 解释:不存在相交的矩形,因此,返回 0 。
提示:
n == bottomLeft.length == topRight.length2 <= n <= 103bottomLeft[i].length == topRight[i].length == 21 <= bottomLeft[i][0], bottomLeft[i][1] <= 1071 <= topRight[i][0], topRight[i][1] <= 107bottomLeft[i][0] < topRight[i][0]bottomLeft[i][1] < topRight[i][1]
解题思路
方法一:枚举
我们可以枚举两个矩形,其中矩形 的左下角和右上角坐标分别为 和 ,矩形 的左下角和右上角坐标分别为 和 。
如果矩形 和矩形 有交集,那么交集的坐标分别为:
- 左下角横坐标是两个矩形左下角横坐标的最大值,即 ;
- 左下角纵坐标是两个矩形左下角纵坐标的最大值,即 ;
- 右上角横坐标是两个矩形右上角横坐标的最小值,即 ;
- 右上角纵坐标是两个矩形右上角纵坐标的最小值,即 。
那么交集的宽和高分别为 和 。我们取两者的最小值作为边长,即 ,如果 ,那么我们就可以得到一个正方形,其面积为 ,我们取所有正方形的最大面积即可。
时间复杂度 ,其中 是矩形的数量。空间复杂度 。
class Solution:
def largestSquareArea(
self, bottomLeft: List[List[int]], topRight: List[List[int]]
) -> int:
ans = 0
for ((x1, y1), (x2, y2)), ((x3, y3), (x4, y4)) in combinations(
zip(bottomLeft, topRight), 2
):
w = min(x2, x4) - max(x1, x3)
h = min(y2, y4) - max(y1, y3)
e = min(w, h)
if e > 0:
ans = max(ans, e * e)
return ans
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Candidate understands how to calculate the intersection of rectangles and how to approach the problem efficiently.
- question_mark
Look for knowledge of computational geometry techniques and optimization strategies for handling large datasets.
- question_mark
The candidate should identify potential trade-offs between brute force and more advanced solutions based on time and space constraints.
常见陷阱
外企场景- error
Not accounting for the case where no intersection exists, which would return a result of 0.
- error
Ignoring the rectangle boundaries when calculating the intersection, which may result in incorrect overlap calculations.
- error
Using brute force without considering optimization, which can lead to performance issues when handling large inputs.
进阶变体
外企场景- arrow_right_alt
Instead of calculating the largest square, the task could ask for the largest rectangle within the intersection.
- arrow_right_alt
The problem could be extended to handle non-rectangular shapes in the intersection, complicating the geometric calculations.
- arrow_right_alt
The problem could involve calculating the largest square inside a single rectangle, with no intersection required.