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.
Subscribe to see which companies asked this question
1 Public classSolution {2 Public intComputearea (intAintBintCintDintEintFintGintH) {3 intTotalarea = (c-a) * (d-b) + (G-E) * (H-F);4 if(A>g | | d<f | | C<e | | B>H)5 returnTotalarea;6 intTMP = (math.min (d,h)-Math.max (B,F)) * (Math.min (C,G)-Math.max (a,e));7 returnTotalarea-tmp;8 }9}
Determine if two rectangles have intersections, and if there are intersections, subtract overlapping. If there is no intersection, the area of the two rectangles is returned directly.
223. Rectangle Area JAVA Solutions