Algorithm
Problem Name: 223. Rectangle Area
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
Code Examples
#1 Code Example with C Programming
Code -
C Programming
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int a, b, c, d, e, f, g, h;
int dup;
int x, y;
if (A < E) {
a = A; b = B; c = C; d = D;
e = E; f = F; g = G; h = H;
} else {
a = E; b = F; c = G; d = H;
e = A; f = B; g = C; h = D;
}
if (h < b || f > d || e > c) {
dup = 0;
} else {
x = c < g ? c - e : g - e;
y = d < h ? d - (b > f ? b : f) : h - (b > f ? b : f);
dup = x * y;
}
return (c - a) * (d - b) + (g - e) * (h - f) - dup;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
lass Solution {
public:
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int areaA = (C - A) * (D - B);
int areaB = (G - E) * (H - F);
if (C < E || A > G || B > H || D < F) return areaA + areaB;
// Bottom left(I, J), Top right(K, L).
int I = max(A, E);
int J = max(B, F);
int K = min(C, G);
int L = min(D, H);
int overlap = (K - I) * (L - J);
return areaA + areaB - overlap;
}
};
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Java Programming
Code -
Java Programming
class Solution {
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int areaOfSqrA = (C - A) * (D - B);
int areaOfSqrB = (G - E) * (H - F);
int left = Math.max(A, E);
int right = Math.min(G, C);
int bottom = Math.max(F, B);
int top = Math.min(D, H);
int overlap = 0;
if(right > left && top > bottom) {
overlap = (right - left) * (top - bottom);
}
return areaOfSqrA + areaOfSqrB - overlap;
}
}
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with Javascript Programming
Code -
Javascript Programming
const computeArea = function(A, B, C, D, E, F, G, H) {
const areaA = (C - A) * (D - B)
const areaB = (G - E) * (H - F)
const intersectionArea =
Math.max(0, Math.min(C, G) - Math.max(A, E)) *
Math.max(0, Math.min(D, H) - Math.max(B, F))
return areaA + areaB - intersectionArea
}
Copy The Code &
Try With Live Editor
Input
Output
#5 Code Example with Python Programming
Code -
Python Programming
class Solution:
def computeArea(self, a, b, c, d, e, f, g, h):
x1, x2, x3 = abs(a - c), abs(e - g), max(a, c, e, g) - min(a, c, e, g)
y1, y2, y3 = abs(b - d), abs(f - h), max(b, d, f, h) - min(b, d, f, h)
if x3 < x1 + x2 and y3 < y1 + y2: intrs = (x1 + x2 - x3) * (y1 + y2 - y3)
else: intrs = 0
return x1 * y1 + x2 * y2 - intrs
Copy The Code &
Try With Live Editor
Input
Output