leetcode
leetcode 2751 ~ 2800
求交集区域内的最大正方形面积

求交集区域内的最大正方形面积

难度:

标签:

题目描述

There exist n rectangles in a 2D plane. You are given two 0-indexed 2D integer arrays bottomLeft and topRight, both of size n x 2, where bottomLeft[i] and topRight[i] represent the bottom-left and top-right coordinates of the ith rectangle respectively.

You can select a region formed from the intersection of two of the given rectangles. You need to find the largest area of a square that can fit inside this region if you select the region optimally.

Return the largest possible area of a square, or 0 if there do not exist any intersecting regions between the rectangles.

 

Example 1:

Input: bottomLeft = [[1,1],[2,2],[3,1]], topRight = [[3,3],[4,4],[6,6]]
Output: 1
Explanation: A square with side length 1 can fit inside either the intersecting region of rectangle 0 and rectangle 1, or the intersecting region of rectangle 1 and rectangle 2. Hence the largest area is side * side which is 1 * 1 == 1.
It can be shown that a square with a greater side length can not fit inside any intersecting region.

Example 2:

Input: bottomLeft = [[1,1],[2,2],[1,2]], topRight = [[3,3],[4,4],[3,4]]
Output: 1
Explanation: A square with side length 1 can fit inside either the intersecting region of rectangle 0 and rectangle 1, the intersecting region of rectangle 1 and rectangle 2, or the intersection region of all 3 rectangles. Hence the largest area is side * side which is 1 * 1 == 1.
It can be shown that a square with a greater side length can not fit inside any intersecting region.
Note that the region can be formed by the intersection of more than 2 rectangles.

Example 3:

Input: bottomLeft = [[1,1],[3,3],[3,1]], topRight = [[2,2],[4,4],[4,2]]
Output: 0
Explanation: No pair of rectangles intersect, hence, we return 0.

 

Constraints:

  • n == bottomLeft.length == topRight.length
  • 2 <= n <= 103
  • bottomLeft[i].length == topRight[i].length == 2
  • 1 <= bottomLeft[i][0], bottomLeft[i][1] <= 107
  • 1 <= topRight[i][0], topRight[i][1] <= 107
  • bottomLeft[i][0] < topRight[i][0]
  • bottomLeft[i][1] < topRight[i][1]

代码结果

运行时间: 172 ms, 内存: 16.7 MB


/*
 * 思路:
 * 使用Java Stream API来简化代码逻辑。
 * 我们可以利用嵌套的forEach循环来遍历所有的矩形组合。
 * 对于每对矩形,计算交集区域的坐标并更新最大正方形的边长。
 */

import java.util.stream.IntStream;

public int maxSquareArea(int[][] bottomLeft, int[][] topRight) {
    int n = bottomLeft.length;
    int[] maxSquareSide = {0};

    IntStream.range(0, n).forEach(i -> {
        IntStream.range(i + 1, n).forEach(j -> {
            int leftX = Math.max(bottomLeft[i][0], bottomLeft[j][0]);
            int bottomY = Math.max(bottomLeft[i][1], bottomLeft[j][1]);
            int rightX = Math.min(topRight[i][0], topRight[j][0]);
            int topY = Math.min(topRight[i][1], topRight[j][1]);

            if (leftX < rightX && bottomY < topY) {
                int side = Math.min(rightX - leftX, topY - bottomY);
                maxSquareSide[0] = Math.max(maxSquareSide[0], side);
            }
        });
    });

    return maxSquareSide[0] * maxSquareSide[0];
}

解释

方法:

该题解采用了双重循环的方式,遍历所有矩形对以找出交集。首先,外层循环遍历每个矩形,内层循环则尝试与外层当前矩形找出可能的交集。在双层循环中,首先会检查两个矩形是否有交集,如果没有,则跳过当前的内层循环。如果存在交集,计算交集矩形的左下角和右上角坐标,然后计算此交集的可能最大正方形边长(选择宽度和高度中的较小值)。如果此边长大于已知的最大边长,则更新最大边长。最后,返回最大正方形的面积。

时间复杂度:

O(n^2)

空间复杂度:

O(1)

代码细节讲解

🦆
为什么在判断两个矩形是否有交集时,使用的是坐标的比较条件而不是其他方法?
使用坐标比较条件来判断两个矩形是否有交集是一种直观且高效的方法。由于矩形的对角坐标已知,可以通过比较这些坐标来快速确定两个矩形是否重叠。具体来说,通过比较一个矩形的右上角坐标是否在另一个矩形的左下角坐标的左侧或下侧,以及一个矩形的左下角坐标是否在另一个矩形的右上角坐标的右侧或上侧,可以直接判定两者是否重叠。这种方法不需要额外的数据结构或复杂计算,因此运行效率高,易于实现。
🦆
在计算交集区域的左下角和右上角坐标时,为什么选择使用max和min函数?这种选择对结果有什么影响?
在计算两个有交集矩形的交集区域时,使用max函数确定交集区域的左下角坐标,使用min函数确定交集区域的右上角坐标,是为了正确获得重叠区域的边界。max函数用于比较两个矩形左下角的横坐标和纵坐标,选取较大的值,确保交集区域不会超出任何一个矩形的左下边界。同理,min函数比较两个矩形右上角的横坐标和纵坐标,选取较小的值,确保交集区域不会超出任何一个矩形的右上边界。这种方法确保了计算出的交集区域是两个矩形实际重叠的部分,且此区域是最大可能的重叠矩形,对求解问题至关重要。
🦆
在双重循环中,外层循环为何从n-1开始而非从0开始?这种选择对算法效率有何影响?
实际上,外层循环在代码中是从0开始,直至n-1,这意味着它遍历每个矩形,以便与其后的每个其他矩形进行比较。这是一个常见的技巧,在处理成对比较时避免重复和不必要的比较。例如,当外层循环的索引为i时,内层循环的索引从i+1开始,确保每对矩形只比较一次。这种做法提高了算法的效率,避免了冗余计算,因为每对矩形只被比较一次而不是两次(i与j和j与i)。

相关问题