梁文峰去年进账50亿,DeepSeek粮草充足
2026/1/18 9:36:04
给定若干轴对齐矩形(用左下角与右上角坐标表示),任选两矩形,取它们的重叠区域。在所有重叠区域中,求能放下的最大正方形面积。
min(交集宽, 交集高),面积为边长平方。min(tx1, tx2) - max(bx1, bx2)与min(ty1, ty2) - max(by1, by2)#include<bits/stdc++.h>usingnamespacestd;classSolution{public:longlonglargestSquareArea(vector<vector<int>>&bottomLeft,vector<vector<int>>&topRight){longlongmax_side=0;intn=(int)bottomLeft.size();for(inti=0;i<n;++i){for(intj=0;j<i;++j){intbx1=bottomLeft[i][0],by1=bottomLeft[i][1];inttx1=topRight[i][0],ty1=topRight[i][1];intbx2=bottomLeft[j][0],by2=bottomLeft[j][1];inttx2=topRight[j][0],ty2=topRight[j][1];intwidth=min(tx1,tx2)-max(bx1,bx2);intheight=min(ty1,ty2)-max(by1,by2);intside=min(width,height);if(side>0)max_side=max(max_side,(longlong)side);}}returnmax_side*max_side;}};| 输入 | 输出 | 说明 |
|---|---|---|
| bottomLeft = [[1,1],[2,2]], topRight = [[3,3],[4,4]] | 1 | 交集为 1x1,最大正方形面积 1 |
| bottomLeft = [[0,0],[1,0]], topRight = [[2,1],[3,2]] | 0 | 交集高度为 0,无法放正方形 |
| bottomLeft = [[0,0],[2,1],[3,3]], topRight = [[3,3],[4,4],[5,5]] | 1 | 取最优矩形对得到边长 1 |
side > 0才是有效交集。