LeetCode Problem Workspace
Rectangle Area
Calculate the total area covered by two rectangles using geometry, handling overlaps and avoiding double counting efficiently.
2
Topics
6
Code langs
3
Related
Practice Focus
Medium · Math plus Geometry
Answer-first summary
Calculate the total area covered by two rectangles using geometry, handling overlaps and avoiding double counting efficiently.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Math plus Geometry
This problem requires computing the combined area of two rectangles on a 2D plane. The challenge lies in correctly handling overlapping regions to avoid double counting. By calculating individual rectangle areas and subtracting the overlap, you get an accurate total using simple geometric reasoning.
Problem Statement
Given two axis-aligned rectangles in a 2D coordinate plane, return the total area covered by both rectangles. Each rectangle is defined by its bottom-left and top-right corners with integer coordinates.
The first rectangle has corners (ax1, ay1) and (ax2, ay2). The second rectangle has corners (bx1, by1) and (bx2, by2). The rectangles may overlap partially, fully, or not at all. Compute the combined area, ensuring overlapping regions are only counted once.
Examples
Example 1
Input: ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2
Output: 45
Example details omitted.
Example 2
Input: ax1 = -2, ay1 = -2, ax2 = 2, ay2 = 2, bx1 = -2, by1 = -2, bx2 = 2, by2 = 2
Output: 16
Example details omitted.
Constraints
- -104 <= ax1 <= ax2 <= 104
- -104 <= ay1 <= ay2 <= 104
- -104 <= bx1 <= bx2 <= 104
- -104 <= by1 <= by2 <= 104
Solution Approach
Compute Individual Rectangle Areas
Calculate the area of each rectangle separately using the formula (width * height) for both rectangles. Width is the difference between right and left x-coordinates, height is the difference between top and bottom y-coordinates.
Determine Overlapping Area
Find the horizontal and vertical overlap between the rectangles. Horizontal overlap is max(0, min(ax2, bx2) - max(ax1, bx1)), vertical overlap is max(0, min(ay2, by2) - max(ay1, by1)). Multiply these to get the overlapping area.
Combine Areas Safely
Add the two rectangle areas and subtract the overlap area. This ensures any shared region is counted only once, producing the correct total area covered by both rectangles.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
Time complexity is O(1) because only constant arithmetic operations are needed. Space complexity is O(1) since no additional data structures are used beyond a few variables for coordinates and overlap calculation.
What Interviewers Usually Probe
- Expect clear handling of overlapping regions.
- Look for edge cases where rectangles touch but do not overlap.
- Check that the solution avoids negative overlap or double counting.
Common Pitfalls or Variants
Common pitfalls
- Forgetting to subtract the overlapping area, leading to inflated totals.
- Incorrectly calculating overlap when rectangles do not intersect.
- Confusing width and height computations with coordinate differences.
Follow-up variants
- Computing total area for multiple rectangles, not just two.
- Handling rectangles with floating-point coordinates instead of integers.
- Finding the overlapping area only without computing total area.
FAQ
How do I compute the total area of two rectangles in the Rectangle Area problem?
Calculate each rectangle's area, compute any overlapping area, and subtract the overlap from the sum of both rectangles.
What if the rectangles do not overlap at all?
If there is no overlap, the overlap calculation will yield zero, and the total area is simply the sum of the two rectangle areas.
Can coordinates be negative?
Yes, rectangles can have negative coordinates as long as the bottom-left corner is less than or equal to the top-right corner.
How to handle rectangles touching at edges?
Edge-touching rectangles have zero overlap. Ensure your overlap formula returns max(0, overlap) to avoid negative values.
Does this approach work for more than two rectangles?
The same pattern applies, but you must iteratively calculate overlaps for all pairs to avoid double counting.
Solution
Solution 1: Calculate Overlapping Area
First, we calculate the area of the two rectangles separately, denoted as $a$ and $b$. Then we calculate the overlapping width $width$ and height $height$. The overlapping area is $max(width, 0) \times max(height, 0)$. Finally, we subtract the overlapping area from $a$ and $b$.
class Solution:
def computeArea(
self,
ax1: int,
ay1: int,
ax2: int,
ay2: int,
bx1: int,
by1: int,
bx2: int,
by2: int,
) -> int:
a = (ax2 - ax1) * (ay2 - ay1)
b = (bx2 - bx1) * (by2 - by1)
width = min(ax2, bx2) - max(ax1, bx1)
height = min(ay2, by2) - max(ay1, by1)
return a + b - max(height, 0) * max(width, 0)Continue Practicing
Continue Topic
math
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
Math plus Geometry
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Medium
Stay on this level to stabilize interview delivery.
arrow_forward