Skip to content

Commit e8a57ef

Browse files
authored
feat: add solutions to lc problem: No.3047 (#2388)
No.3047.Find the Largest Area of Square Inside Two Rectangles
1 parent d773da5 commit e8a57ef

File tree

7 files changed

+305
-8
lines changed

7 files changed

+305
-8
lines changed

solution/3000-3099/3047.Find the Largest Area of Square Inside Two Rectangles/README.md

+107-4
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,127 @@
6161

6262
## 解法
6363

64-
### 方法一
64+
### 方法一:枚举
65+
66+
我们可以枚举两个矩形,其中矩形 $1$ 的左下角和右上角坐标分别为 $(x_1, y_1)$ 和 $(x_2, y_2)$,矩形 $2$ 的左下角和右上角坐标分别为 $(x_3, y_3)$ 和 $(x_4, y_4)$。
67+
68+
如果矩形 $1$ 和矩形 $2$ 有交集,那么交集的坐标分别为:
69+
70+
- 左下角横坐标是两个矩形左下角横坐标的最大值,即 $\max(x_1, x_3)$;
71+
- 左下角纵坐标是两个矩形左下角纵坐标的最大值,即 $\max(y_1, y_3)$;
72+
- 右上角横坐标是两个矩形右上角横坐标的最小值,即 $\min(x_2, x_4)$;
73+
- 右上角纵坐标是两个矩形右上角纵坐标的最小值,即 $\min(y_2, y_4)$。
74+
75+
那么交集的宽和高分别为 $w = \min(x_2, x_4) - \max(x_1, x_3)$ 和 $h = \min(y_2, y_4) - \max(y_1, y_3)$。我们取两者的最小值作为边长,即 $e = \min(w, h)$,如果 $e > 0$,那么我们就可以得到一个正方形,其面积为 $e^2$,我们取所有正方形的最大面积即可。
76+
77+
时间复杂度 $O(n^2)$,其中 $n$ 是矩形的数量。空间复杂度 $O(1)$。
6578

6679
<!-- tabs:start -->
6780

6881
```python
69-
82+
class Solution:
83+
def largestSquareArea(
84+
self, bottomLeft: List[List[int]], topRight: List[List[int]]
85+
) -> int:
86+
ans = 0
87+
for ((x1, y1), (x2, y2)), ((x3, y3), (x4, y4)) in combinations(
88+
zip(bottomLeft, topRight), 2
89+
):
90+
w = min(x2, x4) - max(x1, x3)
91+
h = min(y2, y4) - max(y1, y3)
92+
e = min(w, h)
93+
if e > 0:
94+
ans = max(ans, e * e)
95+
return ans
7096
```
7197

7298
```java
73-
99+
class Solution {
100+
public long largestSquareArea(int[][] bottomLeft, int[][] topRight) {
101+
long ans = 0;
102+
for (int i = 0; i < bottomLeft.length; ++i) {
103+
int x1 = bottomLeft[i][0], y1 = bottomLeft[i][1];
104+
int x2 = topRight[i][0], y2 = topRight[i][1];
105+
for (int j = i + 1; j < bottomLeft.length; ++j) {
106+
int x3 = bottomLeft[j][0], y3 = bottomLeft[j][1];
107+
int x4 = topRight[j][0], y4 = topRight[j][1];
108+
int w = Math.min(x2, x4) - Math.max(x1, x3);
109+
int h = Math.min(y2, y4) - Math.max(y1, y3);
110+
int e = Math.min(w, h);
111+
if (e > 0) {
112+
ans = Math.max(ans, 1L * e * e);
113+
}
114+
}
115+
}
116+
return ans;
117+
}
118+
}
74119
```
75120

76121
```cpp
77-
122+
class Solution {
123+
public:
124+
long long largestSquareArea(vector<vector<int>>& bottomLeft, vector<vector<int>>& topRight) {
125+
long long ans = 0;
126+
for (int i = 0; i < bottomLeft.size(); ++i) {
127+
int x1 = bottomLeft[i][0], y1 = bottomLeft[i][1];
128+
int x2 = topRight[i][0], y2 = topRight[i][1];
129+
for (int j = i + 1; j < bottomLeft.size(); ++j) {
130+
int x3 = bottomLeft[j][0], y3 = bottomLeft[j][1];
131+
int x4 = topRight[j][0], y4 = topRight[j][1];
132+
int w = min(x2, x4) - max(x1, x3);
133+
int h = min(y2, y4) - max(y1, y3);
134+
int e = min(w, h);
135+
if (e > 0) {
136+
ans = max(ans, 1LL * e * e);
137+
}
138+
}
139+
}
140+
return ans;
141+
}
142+
};
78143
```
79144
80145
```go
146+
func largestSquareArea(bottomLeft [][]int, topRight [][]int) (ans int64) {
147+
for i, b1 := range bottomLeft {
148+
t1 := topRight[i]
149+
x1, y1 := b1[0], b1[1]
150+
x2, y2 := t1[0], t1[1]
151+
for j := i + 1; j < len(bottomLeft); j++ {
152+
x3, y3 := bottomLeft[j][0], bottomLeft[j][1]
153+
x4, y4 := topRight[j][0], topRight[j][1]
154+
w := min(x2, x4) - max(x1, x3)
155+
h := min(y2, y4) - max(y1, y3)
156+
e := min(w, h)
157+
if e > 0 {
158+
ans = max(ans, int64(e)*int64(e))
159+
}
160+
}
161+
}
162+
return
163+
}
164+
```
81165

166+
```ts
167+
function largestSquareArea(bottomLeft: number[][], topRight: number[][]): number {
168+
let ans = 0;
169+
for (let i = 0; i < bottomLeft.length; ++i) {
170+
const [x1, y1] = bottomLeft[i];
171+
const [x2, y2] = topRight[i];
172+
for (let j = i + 1; j < bottomLeft.length; ++j) {
173+
const [x3, y3] = bottomLeft[j];
174+
const [x4, y4] = topRight[j];
175+
const w = Math.min(x2, x4) - Math.max(x1, x3);
176+
const h = Math.min(y2, y4) - Math.max(y1, y3);
177+
const e = Math.min(w, h);
178+
if (e > 0) {
179+
ans = Math.max(ans, e * e);
180+
}
181+
}
182+
}
183+
return ans;
184+
}
82185
```
83186

84187
<!-- tabs:end -->

solution/3000-3099/3047.Find the Largest Area of Square Inside Two Rectangles/README_EN.md

+107-4
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,127 @@ Note that the region can be formed by the intersection of more than 2 rectangles
5555

5656
## Solutions
5757

58-
### Solution 1
58+
### Solution 1: Enumeration
59+
60+
We can enumerate two rectangles, where the coordinates of the bottom left and top right corners of rectangle 1 are $(x_1, y_1)$ and $(x_2, y_2)$ respectively, and the coordinates of the bottom left and top right corners of rectangle 2 are $(x_3, y_3)$ and $(x_4, y_4)$ respectively.
61+
62+
If rectangle 1 and rectangle 2 intersect, then the coordinates of the intersection are:
63+
64+
- The x-coordinate of the bottom left corner is the maximum of the x-coordinates of the bottom left corners of the two rectangles, i.e., $\max(x_1, x_3)$;
65+
- The y-coordinate of the bottom left corner is the maximum of the y-coordinates of the bottom left corners of the two rectangles, i.e., $\max(y_1, y_3)$;
66+
- The x-coordinate of the top right corner is the minimum of the x-coordinates of the top right corners of the two rectangles, i.e., $\min(x_2, x_4)$;
67+
- The y-coordinate of the top right corner is the minimum of the y-coordinates of the top right corners of the two rectangles, i.e., $\min(y_2, y_4)$.
68+
69+
Then the width and height of the intersection are $w = \min(x_2, x_4) - \max(x_1, x_3)$ and $h = \min(y_2, y_4) - \max(y_1, y_3)$ respectively. We take the minimum of the two as the side length, i.e., $e = \min(w, h)$. If $e > 0$, then we can get a square with an area of $e^2$. We take the maximum area of all squares.
70+
71+
The time complexity is $O(n^2)$, where $n$ is the number of rectangles. The space complexity is $O(1)$.
5972

6073
<!-- tabs:start -->
6174

6275
```python
63-
76+
class Solution:
77+
def largestSquareArea(
78+
self, bottomLeft: List[List[int]], topRight: List[List[int]]
79+
) -> int:
80+
ans = 0
81+
for ((x1, y1), (x2, y2)), ((x3, y3), (x4, y4)) in combinations(
82+
zip(bottomLeft, topRight), 2
83+
):
84+
w = min(x2, x4) - max(x1, x3)
85+
h = min(y2, y4) - max(y1, y3)
86+
e = min(w, h)
87+
if e > 0:
88+
ans = max(ans, e * e)
89+
return ans
6490
```
6591

6692
```java
67-
93+
class Solution {
94+
public long largestSquareArea(int[][] bottomLeft, int[][] topRight) {
95+
long ans = 0;
96+
for (int i = 0; i < bottomLeft.length; ++i) {
97+
int x1 = bottomLeft[i][0], y1 = bottomLeft[i][1];
98+
int x2 = topRight[i][0], y2 = topRight[i][1];
99+
for (int j = i + 1; j < bottomLeft.length; ++j) {
100+
int x3 = bottomLeft[j][0], y3 = bottomLeft[j][1];
101+
int x4 = topRight[j][0], y4 = topRight[j][1];
102+
int w = Math.min(x2, x4) - Math.max(x1, x3);
103+
int h = Math.min(y2, y4) - Math.max(y1, y3);
104+
int e = Math.min(w, h);
105+
if (e > 0) {
106+
ans = Math.max(ans, 1L * e * e);
107+
}
108+
}
109+
}
110+
return ans;
111+
}
112+
}
68113
```
69114

70115
```cpp
71-
116+
class Solution {
117+
public:
118+
long long largestSquareArea(vector<vector<int>>& bottomLeft, vector<vector<int>>& topRight) {
119+
long long ans = 0;
120+
for (int i = 0; i < bottomLeft.size(); ++i) {
121+
int x1 = bottomLeft[i][0], y1 = bottomLeft[i][1];
122+
int x2 = topRight[i][0], y2 = topRight[i][1];
123+
for (int j = i + 1; j < bottomLeft.size(); ++j) {
124+
int x3 = bottomLeft[j][0], y3 = bottomLeft[j][1];
125+
int x4 = topRight[j][0], y4 = topRight[j][1];
126+
int w = min(x2, x4) - max(x1, x3);
127+
int h = min(y2, y4) - max(y1, y3);
128+
int e = min(w, h);
129+
if (e > 0) {
130+
ans = max(ans, 1LL * e * e);
131+
}
132+
}
133+
}
134+
return ans;
135+
}
136+
};
72137
```
73138
74139
```go
140+
func largestSquareArea(bottomLeft [][]int, topRight [][]int) (ans int64) {
141+
for i, b1 := range bottomLeft {
142+
t1 := topRight[i]
143+
x1, y1 := b1[0], b1[1]
144+
x2, y2 := t1[0], t1[1]
145+
for j := i + 1; j < len(bottomLeft); j++ {
146+
x3, y3 := bottomLeft[j][0], bottomLeft[j][1]
147+
x4, y4 := topRight[j][0], topRight[j][1]
148+
w := min(x2, x4) - max(x1, x3)
149+
h := min(y2, y4) - max(y1, y3)
150+
e := min(w, h)
151+
if e > 0 {
152+
ans = max(ans, int64(e)*int64(e))
153+
}
154+
}
155+
}
156+
return
157+
}
158+
```
75159

160+
```ts
161+
function largestSquareArea(bottomLeft: number[][], topRight: number[][]): number {
162+
let ans = 0;
163+
for (let i = 0; i < bottomLeft.length; ++i) {
164+
const [x1, y1] = bottomLeft[i];
165+
const [x2, y2] = topRight[i];
166+
for (let j = i + 1; j < bottomLeft.length; ++j) {
167+
const [x3, y3] = bottomLeft[j];
168+
const [x4, y4] = topRight[j];
169+
const w = Math.min(x2, x4) - Math.max(x1, x3);
170+
const h = Math.min(y2, y4) - Math.max(y1, y3);
171+
const e = Math.min(w, h);
172+
if (e > 0) {
173+
ans = Math.max(ans, e * e);
174+
}
175+
}
176+
}
177+
return ans;
178+
}
76179
```
77180

78181
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
long long largestSquareArea(vector<vector<int>>& bottomLeft, vector<vector<int>>& topRight) {
4+
long long ans = 0;
5+
for (int i = 0; i < bottomLeft.size(); ++i) {
6+
int x1 = bottomLeft[i][0], y1 = bottomLeft[i][1];
7+
int x2 = topRight[i][0], y2 = topRight[i][1];
8+
for (int j = i + 1; j < bottomLeft.size(); ++j) {
9+
int x3 = bottomLeft[j][0], y3 = bottomLeft[j][1];
10+
int x4 = topRight[j][0], y4 = topRight[j][1];
11+
int w = min(x2, x4) - max(x1, x3);
12+
int h = min(y2, y4) - max(y1, y3);
13+
int e = min(w, h);
14+
if (e > 0) {
15+
ans = max(ans, 1LL * e * e);
16+
}
17+
}
18+
}
19+
return ans;
20+
}
21+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
func largestSquareArea(bottomLeft [][]int, topRight [][]int) (ans int64) {
2+
for i, b1 := range bottomLeft {
3+
t1 := topRight[i]
4+
x1, y1 := b1[0], b1[1]
5+
x2, y2 := t1[0], t1[1]
6+
for j := i + 1; j < len(bottomLeft); j++ {
7+
x3, y3 := bottomLeft[j][0], bottomLeft[j][1]
8+
x4, y4 := topRight[j][0], topRight[j][1]
9+
w := min(x2, x4) - max(x1, x3)
10+
h := min(y2, y4) - max(y1, y3)
11+
e := min(w, h)
12+
if e > 0 {
13+
ans = max(ans, int64(e)*int64(e))
14+
}
15+
}
16+
}
17+
return
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public long largestSquareArea(int[][] bottomLeft, int[][] topRight) {
3+
long ans = 0;
4+
for (int i = 0; i < bottomLeft.length; ++i) {
5+
int x1 = bottomLeft[i][0], y1 = bottomLeft[i][1];
6+
int x2 = topRight[i][0], y2 = topRight[i][1];
7+
for (int j = i + 1; j < bottomLeft.length; ++j) {
8+
int x3 = bottomLeft[j][0], y3 = bottomLeft[j][1];
9+
int x4 = topRight[j][0], y4 = topRight[j][1];
10+
int w = Math.min(x2, x4) - Math.max(x1, x3);
11+
int h = Math.min(y2, y4) - Math.max(y1, y3);
12+
int e = Math.min(w, h);
13+
if (e > 0) {
14+
ans = Math.max(ans, 1L * e * e);
15+
}
16+
}
17+
}
18+
return ans;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def largestSquareArea(
3+
self, bottomLeft: List[List[int]], topRight: List[List[int]]
4+
) -> int:
5+
ans = 0
6+
for ((x1, y1), (x2, y2)), ((x3, y3), (x4, y4)) in combinations(
7+
zip(bottomLeft, topRight), 2
8+
):
9+
w = min(x2, x4) - max(x1, x3)
10+
h = min(y2, y4) - max(y1, y3)
11+
e = min(w, h)
12+
if e > 0:
13+
ans = max(ans, e * e)
14+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function largestSquareArea(bottomLeft: number[][], topRight: number[][]): number {
2+
let ans = 0;
3+
for (let i = 0; i < bottomLeft.length; ++i) {
4+
const [x1, y1] = bottomLeft[i];
5+
const [x2, y2] = topRight[i];
6+
for (let j = i + 1; j < bottomLeft.length; ++j) {
7+
const [x3, y3] = bottomLeft[j];
8+
const [x4, y4] = topRight[j];
9+
const w = Math.min(x2, x4) - Math.max(x1, x3);
10+
const h = Math.min(y2, y4) - Math.max(y1, y3);
11+
const e = Math.min(w, h);
12+
if (e > 0) {
13+
ans = Math.max(ans, e * e);
14+
}
15+
}
16+
}
17+
return ans;
18+
}

0 commit comments

Comments
 (0)