1
class Solution {
2
public:
3
int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
4
int rec1 = abs(ax2 - ax1) * abs(ay2 - ay1); // Area(Rectangle 1)
5
int rec2 = abs(bx2 - bx1) * abs(by2 - by1); // Area(Rectangle 2)
6

7
// As explained above, if intervals overlap, max(x1,x3) < min(x2,x4) and
8
// overlapped interval is ( max(x1,x3) , min(x2,x4) ).
9

10
int ox1 = (max(ax1, bx1) - min(ax2,
11
bx2)); // if ox1 is negative, abs(ox1) is the length of
12
// overlapped rectangle, else rectangles do not overlap.
13
int oy1 = (max(ay1, by1) - min(ay2, by2)); // breadth of overlapped
14
// rectangle
15

16
int rec3 = 0; // if rectangles do not overlap, area of overlapped rectangle
17
// is zero.
18
if (ox1 < 0 && oy1 < 0) // if both ox1 and oy2 are negative, two rectangles overlap.
19
rec3 = ox1 * oy1;
20
return rec1 + rec2 - rec3; // Area(Rectangle 1) + Area(Rectangle 2) -
21
// Area(Overlapped triangle)
22
}
23
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0