Skip to content

Commit b949667

Browse files
chaimode0xMashiro
authored andcommitted
feat: Solve LeetCode Daily Challenge #3047
1 parent 21648ea commit b949667

3 files changed

Lines changed: 132 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
#include "leetcode/core.h"
3+
4+
namespace leetcode {
5+
namespace problem_3047 {
6+
7+
using Func = std::function<long long(vector<vector<int>>&, vector<vector<int>>&)>;
8+
9+
class FindTheLargestAreaOfSquareInsideTwoRectanglesSolution : public SolutionBase<Func> {
10+
public:
11+
//! 3047. Find the Largest Area of Square Inside Two Rectangles
12+
//! https://leetcode.com/problems/find-the-largest-area-of-square-inside-two-rectangles/
13+
long long largestSquareArea(vector<vector<int>>& bottomLeft, vector<vector<int>>& topRight);
14+
15+
FindTheLargestAreaOfSquareInsideTwoRectanglesSolution();
16+
};
17+
18+
} // namespace problem_3047
19+
} // namespace leetcode
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
#include "leetcode/problems/find-the-largest-area-of-square-inside-two-rectangles.h"
3+
4+
namespace leetcode {
5+
namespace problem_3047 {
6+
7+
// 枚举所有矩形对,计算相交区域的最大正方形面积
8+
// 时间复杂度: O(n²), 空间复杂度: O(1)
9+
static long long solution1(vector<vector<int>>& bottomLeft, vector<vector<int>>& topRight) {
10+
const int n = bottomLeft.size();
11+
long long maxArea = 0;
12+
for (int i = 0; i < n; ++i) {
13+
for (int j = i + 1; j < n; ++j) {
14+
// 计算相交区域的左下角和右上角
15+
int x1 = max(bottomLeft[i][0], bottomLeft[j][0]);
16+
int y1 = max(bottomLeft[i][1], bottomLeft[j][1]);
17+
int x2 = min(topRight[i][0], topRight[j][0]);
18+
int y2 = min(topRight[i][1], topRight[j][1]);
19+
// 检查是否相交
20+
if (x1 < x2 && y1 < y2) {
21+
long long width = x2 - x1;
22+
long long height = y2 - y1;
23+
long long side = min(width, height);
24+
long long area = side * side;
25+
if (area > maxArea) {
26+
maxArea = area;
27+
}
28+
}
29+
}
30+
}
31+
return maxArea;
32+
}
33+
34+
FindTheLargestAreaOfSquareInsideTwoRectanglesSolution::FindTheLargestAreaOfSquareInsideTwoRectanglesSolution() {
35+
setMetaInfo({.id = 3047,
36+
.title = "Find the Largest Area of Square Inside Two Rectangles",
37+
.url = "https://leetcode.com/problems/find-the-largest-area-of-square-inside-two-rectangles/"});
38+
registerStrategy("Pairwise Enumeration", solution1);
39+
}
40+
41+
long long FindTheLargestAreaOfSquareInsideTwoRectanglesSolution::largestSquareArea(vector<vector<int>>& bottomLeft, vector<vector<int>>& topRight) {
42+
return getSolution()(bottomLeft, topRight);
43+
}
44+
45+
} // namespace problem_3047
46+
} // namespace leetcode
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
#include "leetcode/problems/find-the-largest-area-of-square-inside-two-rectangles.h"
3+
4+
#include "gtest/gtest.h"
5+
6+
namespace leetcode {
7+
namespace problem_3047 {
8+
9+
class FindTheLargestAreaOfSquareInsideTwoRectanglesTest : public ::testing::TestWithParam<string> {
10+
protected:
11+
void SetUp() override { solution.setStrategy(GetParam()); }
12+
13+
FindTheLargestAreaOfSquareInsideTwoRectanglesSolution solution;
14+
};
15+
16+
TEST_P(FindTheLargestAreaOfSquareInsideTwoRectanglesTest, Example1) {
17+
vector<vector<int>> bottomLeft = {{1, 1}, {2, 2}, {3, 1}};
18+
vector<vector<int>> topRight = {{3, 3}, {4, 4}, {6, 6}};
19+
EXPECT_EQ(solution.largestSquareArea(bottomLeft, topRight), 1);
20+
}
21+
22+
TEST_P(FindTheLargestAreaOfSquareInsideTwoRectanglesTest, Example2) {
23+
vector<vector<int>> bottomLeft = {{1, 1}, {1, 3}, {1, 5}};
24+
vector<vector<int>> topRight = {{5, 5}, {5, 7}, {5, 9}};
25+
EXPECT_EQ(solution.largestSquareArea(bottomLeft, topRight), 4);
26+
}
27+
28+
TEST_P(FindTheLargestAreaOfSquareInsideTwoRectanglesTest, Example3) {
29+
vector<vector<int>> bottomLeft = {{1, 1}, {2, 2}, {1, 2}};
30+
vector<vector<int>> topRight = {{3, 3}, {4, 4}, {3, 4}};
31+
EXPECT_EQ(solution.largestSquareArea(bottomLeft, topRight), 1);
32+
}
33+
34+
TEST_P(FindTheLargestAreaOfSquareInsideTwoRectanglesTest, Example4) {
35+
vector<vector<int>> bottomLeft = {{1, 1}, {3, 3}, {3, 1}};
36+
vector<vector<int>> topRight = {{2, 2}, {4, 4}, {4, 2}};
37+
EXPECT_EQ(solution.largestSquareArea(bottomLeft, topRight), 0);
38+
}
39+
40+
// 添加一些额外的测试用例
41+
TEST_P(FindTheLargestAreaOfSquareInsideTwoRectanglesTest, NoIntersection) {
42+
vector<vector<int>> bottomLeft = {{1, 1}, {5, 5}};
43+
vector<vector<int>> topRight = {{2, 2}, {6, 6}};
44+
EXPECT_EQ(solution.largestSquareArea(bottomLeft, topRight), 0);
45+
}
46+
47+
TEST_P(FindTheLargestAreaOfSquareInsideTwoRectanglesTest, PerfectOverlap) {
48+
vector<vector<int>> bottomLeft = {{1, 1}, {1, 1}};
49+
vector<vector<int>> topRight = {{5, 5}, {5, 5}};
50+
// 宽度和高度都是4,最大正方形边长4,面积16
51+
EXPECT_EQ(solution.largestSquareArea(bottomLeft, topRight), 16);
52+
}
53+
54+
TEST_P(FindTheLargestAreaOfSquareInsideTwoRectanglesTest, LargeCoordinates) {
55+
vector<vector<int>> bottomLeft = {{1, 1}, {1000000, 1000000}};
56+
vector<vector<int>> topRight = {{10000000, 10000000}, {2000000, 2000000}};
57+
// 相交区域: x1 = max(1,1000000)=1000000, y1=1000000, x2=min(10000000,2000000)=2000000, y2=min(10000000,2000000)=2000000
58+
// 宽度=1000000, 高度=1000000, 边长=1000000, 面积=10^12
59+
EXPECT_EQ(solution.largestSquareArea(bottomLeft, topRight), 1000000000000LL);
60+
}
61+
62+
INSTANTIATE_TEST_SUITE_P(
63+
LeetCode, FindTheLargestAreaOfSquareInsideTwoRectanglesTest,
64+
::testing::ValuesIn(FindTheLargestAreaOfSquareInsideTwoRectanglesSolution().getStrategyNames()));
65+
66+
} // namespace problem_3047
67+
} // namespace leetcode

0 commit comments

Comments
 (0)