LeetCode – Rectangle Area (Java)
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 coordinates.
Analysis
This problem can be converted as a overlap internal problem. On the xaxis, there are (A,C) and (E,G); on the yaxis, there are (F,H) and (B,D). If they do not have overlap, the total area is the sum of 2 rectangle areas. If they have overlap, the total area should minus the overlap area.
Java Solution
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { if(C<EG<A ) return (GE)*(HF) + (CA)*(DB); if(D<F  H<B) return (GE)*(HF) + (CA)*(DB); int right = Math.min(C,G); int left = Math.max(A,E); int top = Math.min(H,D); int bottom = Math.max(F,B); return (GE)*(HF) + (CA)*(DB)  (rightleft)*(topbottom); } 
<pre><code> String foo = "bar"; </code></pre>

TiyaChu Max

Ameya Naik

w

Wang Sysc Sysc

Suzy

Dawei