Find the total area covered by two rectilinear rectangles in a 2D plane.
Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.
Rectangle Area
Assume that the total area is never beyond the maximum possible value of int.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Solution { public: int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { int maxae = A > E ? A : E; int mincg = C > G ? G : C; int maxfb = F > B ? F : B; int minhd = H > D ? D : H; int overlap; if(maxae >= mincg || maxfb >= minhd) overlap = 0; else overlap = (mincg - maxae) * (minhd - maxfb); return (C - A) * (D - B) + (G - E) * (H - F) - overlap; } }; |
❤ 点击这里 -> 订阅《PAT | 蓝桥 | LeetCode学习路径 & 刷题经验》by 柳婼