My straight and easy understand Java solution


  • 0
    J
    public class Solution {
        public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
            int sumA = (C-A) * (D - B);
            int sumB = (G - E) * (H - F);
            int overLap = 0;
            
            if(E < C && G > A && F < D && H> B){
                int leftBottomX = Math.max(A,E);
                int leftBottomY = Math.max(B,F);
                int rightTopX = Math.min(C,G);
                int rightTopY = Math.min(D,H);
                overLap = (rightTopX - leftBottomX) * (rightTopY - leftBottomY);
            }
            return sumA + sumB - overLap;
        }
    }

Log in to reply
 

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