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: [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
Step 1: Individual PiecesStep 2: Perfect AssemblyPerfect RectangleCorner Tracking ProcessHash Set Operations:1. Add corner → count = 12. Add same corner → count = 2 → remove3. Internal corners cancel out4. Only boundary corners remain✓ Valid: Exactly 4 corners left✗ Invalid: Wrong number of cornersTime: O(n) | Space: O(n)🎯 Key Insight: Perfect rectangles have exactly 4 unique boundary corners after internal cancellation
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

n
2n
Linear Growth
Space Complexity
O(n)

Hash set can contain up to 4n corner points in worst case (though typically much less)

n
2n
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
Asked in
Google 23 Amazon 18 Microsoft 12 Apple 8
28.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen