Java instructive


  • 0
    B
    public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        
        int area1 = (int)Math.abs(C-A)*(int)Math.abs(D-B);
        int area2 = (int)Math.abs(G-E)*(int)Math.abs(H-F);
        
        // if separate then just return two areas
        if (A>G || C<E || B>H || D<F)
            return area1+area2;
        
        // calculate boundary of intersecting rect
        int minX = Math.max(A,E);
        int maxX = Math.min(C,G);
        
        int minY = Math.max(B,F);
        int maxY = Math.min(D,H);
       
        int intArea = (maxX-minX)*(maxY-minY);
        
        // subtract intersection from total
        return area1+area2-intArea;
    }

Log in to reply
 

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