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 <= 104
This problem asks to compute the total area covered by two rectangles given their bottom-left and top-right corner coordinates. The key is to efficiently handle overlapping areas.
Approach:
The solution uses a straightforward approach:
Calculate individual areas: First, compute the area of each rectangle separately. The area of a rectangle is simply (width * height).
Calculate overlapping area: The trickiest part is calculating the overlap. We find the overlapping region by determining the intersection rectangle's width and height.
Overlapping width: The overlapping width is min(ax2, bx2) - max(ax1, bx1)
. This finds the rightmost left coordinate and subtracts the leftmost right coordinate. If this difference is negative, there's no overlap, so we use max(width, 0)
.
Overlapping height: Similarly, the overlapping height is min(ay2, by2) - max(ay1, by1)
. Again, if negative, we use max(height, 0)
.
Total area: The total area is the sum of the individual areas minus the overlapping area (to avoid double-counting).
Time and Space Complexity:
Time Complexity: O(1). All calculations involve a fixed number of arithmetic operations regardless of input size.
Space Complexity: O(1). The algorithm uses a constant amount of extra space to store variables.
Code Implementation (Python):
class Solution:
def computeArea(
self,
ax1: int,
ay1: int,
ax2: int,
ay2: int,
bx1: int,
by1: int,
bx2: int,
by2: int,
) -> int:
# Calculate individual areas
area_a = (ax2 - ax1) * (ay2 - ay1)
area_b = (bx2 - bx1) * (by2 - by1)
# Calculate overlapping width and height
overlap_width = min(ax2, bx2) - max(ax1, bx1)
overlap_height = min(ay2, by2) - max(ay1, by1)
# Handle cases with no overlap
overlap_area = max(0, overlap_width) * max(0, overlap_height)
# Total area: sum of individual areas minus overlap
total_area = area_a + area_b - overlap_area
return total_area
Code Implementation (Java):
class Solution {
public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
long areaA = (long)(ax2 - ax1) * (ay2 - ay1);
long areaB = (long)(bx2 - bx1) * (by2 - by1);
long overlapWidth = Math.min(ax2, bx2) - Math.max(ax1, bx1);
long overlapHeight = Math.min(ay2, by2) - Math.max(ay1, by1);
long overlapArea = Math.max(0, overlapWidth) * Math.max(0, overlapHeight);
return (int)(areaA + areaB - overlapArea);
}
}
Code Implementations in other languages (C++, Go, TypeScript, C#) are similar and follow the same logic. They are provided in the original response. The core idea remains consistent across all languages.