Easy understanding C/C++ solution

  • 2
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
    	int areaA = (C - A)*(D - B);
    	int areaB = (G - E)*(H - F);
    	int total = areaA + areaB;
        //if no intersection, then just return total area
    	if (C <= E || A >= G || D <= F || B >= H)
    		return total;
        //else get the coordinate of intersection at (bottom,left) (top, right) 
    	// (max(a,e), max(f,b)) (min(c,g), min(h,d))
    	int interCection = (min(C, G) - max(A, E))*(min(H, D) - max(F, B));
    	return (total - interCection);

Log in to reply

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