2026/4/5 20:04:56
网站建设
项目流程
哪里有做网站排名优化,网站编辑内容,宁波seo推广,python基础教程视频在二维平面上存在 n 个矩形。给你两个下标从 0 开始的二维整数数组 bottomLeft 和 topRight#xff0c;两个数组的大小都是 n x 2 #xff0c;其中 bottomLeft[i] 和 topRight[i] 分别代表第 i 个矩形的 左下角 和 右上角 坐标。我们定义 向右 的方向为 x 轴正半轴#xff0…在二维平面上存在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 10^3bottomLeft[i].length topRight[i].length 21 bottomLeft[i][0], bottomLeft[i][1] 10^71 topRight[i][0], topRight[i][1] 10^7bottomLeft[i][0] topRight[i][0]bottomLeft[i][1] topRight[i][1]分析检查任意两个矩形是否相交如果相交取横向、纵向相交部分的较小值作为正方形的边最后返回边长相乘的值。判断两个区间是否有重合部分可以先分别计算两个区间的长度如果区间长度之和大于右端点的最大值减去左端点的最小值的差大于 0说明重合。long long largestSquareArea(int** bottomLeft, int bottomLeftSize, int* bottomLeftColSize, int** topRight, int topRightSize, int* topRightColSize) { int nbottomLeftSize,ans0; for(int i0;in;i) { int len_xitopRight[i][0]-bottomLeft[i][0],len_yitopRight[i][1]-bottomLeft[i][1]; for(int ji1;jn;j) { int len_xjtopRight[j][0]-bottomLeft[j][0],len_yjtopRight[j][1]-bottomLeft[j][1]; int l1len_xilen_xj-(fmax(topRight[i][0],topRight[j][0])-fmin(bottomLeft[i][0],bottomLeft[j][0])); int l2len_yilen_yj-(fmax(topRight[i][1],topRight[j][1])-fmin(bottomLeft[i][1],bottomLeft[j][1])); ansfmax(ans,fmin(l1,l2)); } } return 1LL*ans*ans; }