You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/3000-3099/3047.Find the Largest Area of Square Inside Two Rectangles/README_EN.md
+107-4
Original file line number
Diff line number
Diff line change
@@ -55,24 +55,127 @@ Note that the region can be formed by the intersection of more than 2 rectangles
55
55
56
56
## Solutions
57
57
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)$.
0 commit comments