223. Rectangle Area
Problem Description
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:
-10^4 <= ax1 <= ax2 <= 10^4-10^4 <= ay1 <= ay2 <= 10^4-10^4 <= bx1 <= bx2 <= 10^4-10^4 <= by1 <= by2 <= 10^4
Solution
Python Solution
class Solution:
def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int,
bx1: int, by1: int, bx2: int, by2: int) -> int:
# Calculate areas of both rectangles
area1 = (ax2 - ax1) * (ay2 - ay1)
area2 = (bx2 - bx1) * (by2 - by1)
# Find overlapping rectangle coordinates
x_left = max(ax1, bx1)
x_right = min(ax2, bx2)
y_bottom = max(ay1, by1)
y_top = min(ay2, by2)
# Calculate overlapping area
overlap_area = 0
if x_right > x_left and y_top > y_bottom:
overlap_area = (x_right - x_left) * (y_top - y_bottom)
# Total area is sum of both areas minus the overlap
return area1 + area2 - overlap_area
Time Complexity: O(1)
The solution uses simple arithmetic operations.
Space Complexity: O(1)
Only constant extra space is used.
Java Solution
class Solution {
public int computeArea(int ax1, int ay1, int ax2, int ay2,
int bx1, int by1, int bx2, int by2) {
// Calculate areas of both rectangles
int area1 = (ax2 - ax1) * (ay2 - ay1);
int area2 = (bx2 - bx1) * (by2 - by1);
// Find overlapping rectangle coordinates
int xLeft = Math.max(ax1, bx1);
int xRight = Math.min(ax2, bx2);
int yBottom = Math.max(ay1, by1);
int yTop = Math.min(ay2, by2);
// Calculate overlapping area
int overlapArea = 0;
if (xRight > xLeft && yTop > yBottom) {
overlapArea = (xRight - xLeft) * (yTop - yBottom);
}
// Total area is sum of both areas minus the overlap
return area1 + area2 - overlapArea;
}
}
Time Complexity: O(1)
The solution uses simple arithmetic operations.
Space Complexity: O(1)
Only constant extra space is used.
C++ Solution
class Solution {
public:
int computeArea(int ax1, int ay1, int ax2, int ay2,
int bx1, int by1, int bx2, int by2) {
// Calculate areas of both rectangles
long long area1 = (long long)(ax2 - ax1) * (ay2 - ay1);
long long area2 = (long long)(bx2 - bx1) * (by2 - by1);
// Find overlapping rectangle coordinates
int xLeft = max(ax1, bx1);
int xRight = min(ax2, bx2);
int yBottom = max(ay1, by1);
int yTop = min(ay2, by2);
// Calculate overlapping area
long long overlapArea = 0;
if (xRight > xLeft && yTop > yBottom) {
overlapArea = (long long)(xRight - xLeft) * (yTop - yBottom);
}
// Total area is sum of both areas minus the overlap
return (int)(area1 + area2 - overlapArea);
}
};
Time Complexity: O(1)
The solution uses simple arithmetic operations.
Space Complexity: O(1)
Only constant extra space is used.
JavaScript Solution
/**
* @param {number} ax1
* @param {number} ay1
* @param {number} ax2
* @param {number} ay2
* @param {number} bx1
* @param {number} by1
* @param {number} bx2
* @param {number} by2
* @return {number}
*/
var computeArea = function(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2) {
// Calculate areas of both rectangles
const area1 = (ax2 - ax1) * (ay2 - ay1);
const area2 = (bx2 - bx1) * (by2 - by1);
// Find overlapping rectangle coordinates
const xLeft = Math.max(ax1, bx1);
const xRight = Math.min(ax2, bx2);
const yBottom = Math.max(ay1, by1);
const yTop = Math.min(ay2, by2);
// Calculate overlapping area
let overlapArea = 0;
if (xRight > xLeft && yTop > yBottom) {
overlapArea = (xRight - xLeft) * (yTop - yBottom);
}
// Total area is sum of both areas minus the overlap
return area1 + area2 - overlapArea;
};
Time Complexity: O(1)
The solution uses simple arithmetic operations.
Space Complexity: O(1)
Only constant extra space is used.
C# Solution
public class Solution {
public int ComputeArea(int ax1, int ay1, int ax2, int ay2,
int bx1, int by1, int bx2, int by2) {
// Calculate areas of both rectangles
long area1 = (long)(ax2 - ax1) * (ay2 - ay1);
long area2 = (long)(bx2 - bx1) * (by2 - by1);
// Find overlapping rectangle coordinates
int xLeft = Math.Max(ax1, bx1);
int xRight = Math.Min(ax2, bx2);
int yBottom = Math.Max(ay1, by1);
int yTop = Math.Min(ay2, by2);
// Calculate overlapping area
long overlapArea = 0;
if (xRight > xLeft && yTop > yBottom) {
overlapArea = (long)(xRight - xLeft) * (yTop - yBottom);
}
// Total area is sum of both areas minus the overlap
return (int)(area1 + area2 - overlapArea);
}
}
Time Complexity: O(1)
The solution uses simple arithmetic operations.
Space Complexity: O(1)
Only constant extra space is used.
Approach Explanation
The solution follows these steps:
- Calculate the area of each rectangle separately
- Find the coordinates of the overlapping rectangle:
- Left x-coordinate is the maximum of both left x-coordinates
- Right x-coordinate is the minimum of both right x-coordinates
- Bottom y-coordinate is the maximum of both bottom y-coordinates
- Top y-coordinate is the minimum of both top y-coordinates
- Calculate the area of overlap if the rectangles intersect
- Return the sum of both areas minus the overlapping area
The key insight is that we need to subtract the overlapping area exactly once, as it would otherwise be counted twice in the sum of the individual areas.