Find the Largest Area of Square Inside Two Rectangles - Problem
Imagine you're a city planner working with overlapping development zones! You have n rectangles on a 2D plane, each representing a development area with edges parallel to the x and y axes.
You're given two arrays:
bottomLeft[i] = [a_i, b_i]- the bottom-left coordinates of rectangle itopRight[i] = [c_i, d_i]- the top-right coordinates of rectangle i
Your mission: Find the maximum area of a square that can fit inside the intersecting region of at least two rectangles. If no such intersection exists where a square can fit, return 0.
Key Challenge: The square must be entirely contained within the overlapping area of at least two rectangles, and we want the largest possible square area!
Input & Output
example_1.py β Basic Intersection
$
Input:
bottomLeft = [[1,1],[2,2],[3,1]], topRight = [[3,3],[4,4],[4,4]]
βΊ
Output:
1
π‘ Note:
Rectangle 1: (1,1) to (3,3), Rectangle 2: (2,2) to (4,4), Rectangle 3: (3,1) to (4,4). The intersection of rectangles 1 and 2 is (2,2) to (3,3) with area 1Γ1=1. The largest square has area 1.
example_2.py β No Intersection
$
Input:
bottomLeft = [[1,1],[2,2],[1,2]], topRight = [[2,2],[3,3],[3,3]]
βΊ
Output:
1
π‘ Note:
Rectangle 1: (1,1) to (2,2), Rectangle 2: (2,2) to (3,3), Rectangle 3: (1,2) to (3,3). Rectangles 2 and 3 intersect from (2,2) to (3,3), giving a 1Γ1 square with area 1.
example_3.py β Large Intersection
$
Input:
bottomLeft = [[1,1],[3,3],[3,1]], topRight = [[5,5],[6,6],[6,4]]
βΊ
Output:
4
π‘ Note:
Rectangle 1: (1,1) to (5,5), Rectangle 2: (3,3) to (6,6), Rectangle 3: (3,1) to (6,4). The intersection of rectangles 1 and 3 is (3,1) to (5,4) with dimensions 2Γ3. The largest square has side length 2, so area = 4.
Constraints
- n == bottomLeft.length == topRight.length
- 2 β€ n β€ 103
- bottomLeft[i].length == topRight[i].length == 2
- 1 β€ bottomLeft[i][0] < topRight[i][0] β€ 107
- 1 β€ bottomLeft[i][1] < topRight[i][1] β€ 107
- Each rectangle is guaranteed to be valid (bottom-left coordinates are less than top-right)
Visualization
Tap to expand
Understanding the Visualization
1
Setup Rectangles
Place rectangles on 2D coordinate plane
2
Find Intersections
Calculate overlapping regions between rectangle pairs
3
Calculate Squares
For each intersection, find the largest possible square
4
Return Maximum
Select the intersection with the largest square area
Key Takeaway
π― Key Insight: The largest square in any rectangle has side length equal to min(width, height). Check all rectangle pairs to find the maximum square area in their intersections.
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code