Given the coordinates of two rectilinear rectangles in a 2D plane, return the total area covered by the two rectangles.
The first rectangle is defined by its bottom-left corner (ax1, ay1) and its top-right corner (ax2, ay2).
The second rectangle is defined by its bottom-left corner (bx1, by1) and its top-right corner (bx2, by2).
Example 1:
Input: ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2 Output: 45
Example 2:
Input: ax1 = -2, ay1 = -2, ax2 = 2, ay2 = 2, bx1 = -2, by1 = -2, bx2 = 2, by2 = 2 Output: 16
Constraints:
-104 <= ax1 <= ax2 <= 104-104 <= ay1 <= ay2 <= 104-104 <= bx1 <= bx2 <= 104-104 <= by1 <= by2 <= 104When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To find the total area covered by two rectangles, the brute force approach directly checks every single point in a large enough space. We determine if each point is inside either of the rectangles.
Here's how the algorithm would work step-by-step:
def rectangle_area(rectangle_one, rectangle_two):
x_one_rectangle_one, y_one_rectangle_one, x_two_rectangle_one, y_two_rectangle_one = rectangle_one
x_one_rectangle_two, y_one_rectangle_two, x_two_rectangle_two, y_two_rectangle_two = rectangle_two
# Find the bounds of a box containing both rectangles
min_x = min(x_one_rectangle_one, x_one_rectangle_two, x_two_rectangle_one, x_two_rectangle_two)
min_y = min(y_one_rectangle_one, y_one_rectangle_two, y_two_rectangle_one, y_two_rectangle_two)
max_x = max(x_one_rectangle_one, x_one_rectangle_two, x_two_rectangle_one, x_two_rectangle_two)
max_y = max(y_one_rectangle_one, y_one_rectangle_two, y_two_rectangle_one, y_two_rectangle_two)
total_area = 0
# Iterate over all possible points within the bounding box.
for x_coordinate in range(min_x, max_x + 1):
for y_coordinate in range(min_y, max_y + 1):
# Check if the current point is inside rectangle one.
if (x_one_rectangle_one <= x_coordinate <= x_two_rectangle_one) and \
(y_one_rectangle_one <= y_coordinate <= y_two_rectangle_one):
total_area += 1
else:
# Check if the current point is inside rectangle two.
if (x_one_rectangle_two <= x_coordinate <= x_two_rectangle_two) and \
(y_one_rectangle_two <= y_coordinate <= y_two_rectangle_two):
total_area += 1
return total_areaTo find the area covered by two rectangles, we don't need to calculate areas separately and then figure out the overlap. We calculate individual areas and then intelligently figure out the overlapping area (if any) to avoid double-counting.
Here's how the algorithm would work step-by-step:
def calculate_total_area(
rectangle_one_bottom_left_x,
rectangle_one_bottom_left_y,
rectangle_one_top_right_x,
rectangle_one_top_right_y,
rectangle_two_bottom_left_x,
rectangle_two_bottom_left_y,
rectangle_two_top_right_x,
rectangle_two_top_right_y
):
rectangle_one_area = (
rectangle_one_top_right_x - rectangle_one_bottom_left_x
) * (rectangle_one_top_right_y - rectangle_one_bottom_left_y)
rectangle_two_area = (
rectangle_two_top_right_x - rectangle_two_bottom_left_x
) * (rectangle_two_top_right_y - rectangle_two_bottom_left_y)
# Determine the overlap region boundaries
overlap_left_x = max(
rectangle_one_bottom_left_x, rectangle_two_bottom_left_x
)
overlap_right_x = min(
rectangle_one_top_right_x, rectangle_two_top_right_x
)
overlap_bottom_y = max(
rectangle_one_bottom_left_y, rectangle_two_bottom_left_y
)
overlap_top_y = min(
rectangle_one_top_right_y, rectangle_two_top_right_y
)
# If no overlap, return sum of individual areas
if overlap_left_x >= overlap_right_x or overlap_bottom_y >= overlap_top_y:
return rectangle_one_area + rectangle_two_area
# We have overlap, calculate area of overlap
overlap_area = (
overlap_right_x - overlap_left_x
) * (overlap_top_y - overlap_bottom_y)
# Avoid double counting by subtracting overlap
return rectangle_one_area + rectangle_two_area - overlap_area| Case | How to Handle |
|---|---|
| Null or undefined input rectangles | Throw an IllegalArgumentException or return 0, based on the preferred error handling strategy and problem statement requirements. |
| Rectangles with zero width or height | Treat these as having an area of zero and handle calculations appropriately without causing errors. |
| Rectangles with negative coordinates | The problem description should clarify whether negative coordinates are allowed; If allowed, properly compute width/height using absolute difference. |
| Integer overflow when calculating area or sum of areas | Use a larger data type (e.g., long) for intermediate calculations to prevent overflow issues. |
| Rectangles with very large coordinates that could lead to overflow | Ensure all coordinate calculations use long data types to mitigate risk of integer overflow during area calculations. |
| Two rectangles are identical | The intersection area is equal to the area of either rectangle. |
| One rectangle is completely contained within the other | Intersection area is the area of the smaller rectangle. |
| Rectangles that do not overlap at all | The intersection area is zero in this case. |