Judge if has overlapped area first then calculate area


  • 0
    M

    Just share my solution. 5ms time, any faster one?

        public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
            if(noInterval(A,B,C,D,E,F,G,H))    return (C-A)*(D-B)+(G-E)*(H-F);
            int x=Math.min(C,G)-Math.max(A,E);
            int y=Math.min(D,H)-Math.max(B,F);
            return (C-A)*(D-B)+(G-E)*(H-F)-x*y;
        }
        public boolean noInterval(int A, int B, int C, int D, int E, int F, int G, int H){
            return F>D||G<A||H<B||E>C;
        }
    }
    

Log in to reply
 

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