Java Solution: think about rectangle position.


  • 0
    J

    public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {

        if(C<E || A>G || B>H || D<F){  //no overlap position
            return (C-A)*(D-B) + (G-E)*(H-F);
        }
            // find the overlap points as following
            int xmin, ymin, xmax, ymax;
            if(E>A){
                xmin=E;
            } else {
                xmin=A;
            }
            if(B>F) {
                ymin=B;
            }
            else {
                ymin=F;
            }
            if(G<C) {
                xmax=G;
            }
            else {
                xmax=C;
            }
            if(D<H) {
                ymax=D;
            }
            else {
                ymax=H;
            }
            return (C-A)*(D-B) + (G-E)*(H-F) - (xmax-xmin)*(ymax-ymin);
        
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.