Just another c++ solution and taking care of the case (C<A)


  • 1
    X
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        //if C < A ...
        //xi, yi are edge lengths
        int x1 = abs(C-A), y1 = abs(D-B), x2 = abs(G-E), y2 = abs(H-F);
    
        int c = max(max(A,C),max(E,G)),
            a = min(min(A,C),min(E,G)),
            d = max(max(D,B),max(H,F)),
            b = min(min(D,B),min(H,F));//bounding box of the two rectangles
        int x3 = a+x1 < c-x2 ? 0 : (a+x1)-(c-x2),//some how avoid overflow
            y3 = b+y1 < d-y2 ? 0 : (b+y1)-(d-y2);//overlap rectangle
        return (x1*y1-x3*y3+x2*y2);
    }

Log in to reply
 

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