Perfect Rectangle - Problem
Perfect Rectangle is a fascinating geometric puzzle that tests your ability to work with coordinate geometry and hash tables. You're given an array of rectangles, where each rectangle is represented by four coordinates:
Your task is to determine whether all these rectangles together form exactly one perfect rectangular region with no gaps and no overlaps. Think of it like a jigsaw puzzle - all pieces must fit together perfectly to form one complete rectangle.
Key Requirements:
• No gaps between rectangles
• No overlapping areas
• The combined shape must be exactly one rectangle
• All input rectangles must be used
[x1, y1, x2, y2] representing the bottom-left point (x1, y1) and top-right point (x2, y2).Your task is to determine whether all these rectangles together form exactly one perfect rectangular region with no gaps and no overlaps. Think of it like a jigsaw puzzle - all pieces must fit together perfectly to form one complete rectangle.
Key Requirements:
• No gaps between rectangles
• No overlapping areas
• The combined shape must be exactly one rectangle
• All input rectangles must be used
Input & Output
example_1.py — Valid Perfect Rectangle
$
Input:
rectangles = [[1,1,3,3],[3,1,4,2],[3,2,4,4],[1,3,2,4],[2,3,3,4]]
›
Output:
true
💡 Note:
These 5 rectangles perfectly form a 3×3 square from (1,1) to (4,4) with no gaps or overlaps. Each internal corner appears an even number of times, while the four outer corners (1,1), (4,1), (1,4), (4,4) appear exactly once.
example_2.py — Invalid - Has Gap
$
Input:
rectangles = [[1,1,2,3],[1,3,2,4],[3,1,4,2],[3,2,4,4]]
›
Output:
false
💡 Note:
These rectangles don't form a perfect rectangle because there's a gap in the region from (2,1) to (3,4). The total area of rectangles (5) doesn't match the expected bounding rectangle area (9).
example_3.py — Invalid - Has Overlap
$
Input:
rectangles = [[1,1,3,3],[3,1,4,2],[1,3,2,4],[2,2,4,4]]
›
Output:
false
💡 Note:
The last rectangle [2,2,4,4] overlaps with the first rectangle [1,1,3,3] in the region (2,2) to (3,3). This creates overlapping area, violating the perfect rectangle condition.
Visualization
Tap to expand
Understanding the Visualization
1
Collect Pieces
Gather all rectangle pieces and identify their corner points
2
Track Corners
Use a hash set to track how many times each corner point appears
3
Cancel Internal
Internal corners appear even times and cancel out from the set
4
Verify Result
Only 4 outer corners should remain, forming the perfect rectangle boundary
Key Takeaway
🎯 Key Insight: In a perfect rectangle cover, internal corner points appear an even number of times and cancel out, leaving exactly 4 boundary corners that appear once each.
Time & Space Complexity
Time Complexity
O(n)
Single pass through all rectangles, with O(1) hash set operations per rectangle
✓ Linear Growth
Space Complexity
O(n)
Hash set can contain up to 4n corner points in worst case (though typically much less)
⚡ Linearithmic Space
Constraints
- 1 ≤ rectangles.length ≤ 2 × 104
- rectangles[i].length == 4
- -105 ≤ xi, yi, ai, bi ≤ 105
- All rectangles are axis-aligned and have positive area
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code