223. Rectangle Area

LeetCode

Explanation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int areaRectA=(C-A)*(D-B);
int areaRectB=(G-E)*(H-F);

int left=Math.max(A,E);
int right=Math.min(C,G);
int top=Math.min(D,H);
int bottom=Math.max(B,F);

// If overlap
int overlap=0;
if(left<right&&bottom<top) overlap=(right-left)*(top-bottom);

return areaRectA+areaRectB-overlap;
}
}

0%