Rectangle Area
Find the total area covered by and rectilinear rectangles in a 2D plane.
Each rectangle was defined by its bottom left corner and top right corner as shown in the figure.
Assume the total area is never beyond the maximum possible value of int.
Credits:
Special thanks to @mithmatt for adding this problem, creating the above image and all test cases.
Problem Solving Ideas:
The test instructions calculates the total area covered by the two rectangles, calculated as the area of the rectangle 1 + 2 of the rectangle-two rectangles with a common covering area.
Class Solution {public: int computearea (int A, int B, int C, int D, int E, int F, int G, int H) { int overleft = m Ax (A, E); int overbottom = max (B, F); int overright = min (C, G); int overtop = min (D, H); int overarea = 0; if (Overleft>=overright | | overbottom>=overtop) { Overarea = 0; } else{ Overarea = (overright-overleft) * (Overtop-overbottom); } Return (C-A) * (d-b) + (G-E) * (h-f)-Overarea; };
[Leetcode] Rectangle Area