My Java solution [Sum of areas - Overlapped area]

  • 141
    public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
            int areaOfSqrA = (C-A) * (D-B);
             int areaOfSqrB = (G-E) * (H-F);
            int left = Math.max(A, E);
            int right = Math.min(G, C);
            int bottom = Math.max(F, B);
            int top = Math.min(D, H);
            //If overlap
            int overlap = 0;
            if(right > left && top > bottom)
                 overlap = (right - left) * (top - bottom);
            return areaOfSqrA + areaOfSqrB - overlap;

    Hello! So, the code should be fairly straightforward. I first calculate the area of each rectangle and then calculate the overlapping area between the two rectangles (if there is one!). At the end, we sum up the individual areas and subtract the overlapping area/0 !

    Feel free to ask should you have any queries for me OR if my solution can be improved upon! :)

  • 15

    hero has the same opinion!
    Mine is similar with yours :)

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

  • -1

    Same here,

    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        int total = (C-A)*(D-B) + (G-E)*(H-F);
        int L = min(D, H), J = max(B, F);
        int I = max(A, E), K = min(C, G);
        if (L > J && K > I)
            total -= ( (L-J)*(K-I) );
        return total;

  • 0
    public class Solution{
    public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H){
    int overlapLength = Math.max(A,E) < Math.min(C,G) ? Math.min(C,G) - Math.max(A,E) : 0 ;
    int overlapHeight = Math.max(B,F) < Math.min(D,H) ? Math.min(D,H) - Math.max(B,F) : 0;
    return (C-A)*(D-B)+(G-E)*(H-F) - overlapLength*overlapHeight;


  • 1

    What does "Sqr" mean?

  • 0

    Square. It's sort of a habit from high school, I guess. :)

  • 1

    That's what I thought. But where do you have squares here? All I know is that they're rectangles.

  • 0

    Yeah. Should've been areaOfRectA. Totally overlooked the name when I was writing the code. Thanks.

  • 0

    In the first line, int areaOfSqrA = (C-A) * (D-B); is it possible this area is negative ?
    lets say (C,D) = (-2,2) , (A,B ) = ( -1,1), then the area = (-2 -(-1)) * (2-1) = -2, this then makes no sense. Correct me if I am wrong.

  • 0

    Hi! I can see why you would think that but remind yourself on how we usually calculate an area of a RECTANGLE. Width * Height right? Ok. Now, how do we get the width/height out of the coordinates that a rectangle occupy? In this problem (on the 2D plane), C represents the RIGHT-side (X-coordinate) of a rectangle whereas A represents the LEFT-side (X-coordinate) of the rectangle. A can NEVER be HIGHER than C. Try sketching the rectangle out and give it VALID coordinates. You'll see that C >= A. Same goes for D & B for the Y-coordinates. Hope that makes sense. :)

  • 3

    First you called them squares, now you even say triangle... you really don't like rectangles, don't you? :-P

  • 0

    Omg. I don't even know how that happened. Haha. Alright. I've changed it. Hopefully, it's right this time. Thanks for the clarification! :)

  • 0


  • 0

    really brilliant

  • 0

    @waisuan Could you please explain the logic behind finding values of left, right, bottom and top and overlap ? Or if you could point to some resources that will help me understand the logic

  • 0


    Interesting answer. I would like to suggest mine as well.

    In Java, this task can be simply done as follows:

        public int solution(int K, int L, int M, int N, int P, int Q, int R, int S) {
        int areaT_,area1_ ,area2_ ,areaInt_ = 0;
        Rectangle rect_KLMN = new Rectangle(K, L, M - K, N - L);
        Rectangle rect_PQRS = new Rectangle(P, Q, R - P, S - Q);
        area1_ = (int) (rect_KLMN.height * rect_KLMN.width);
        area2_ = (int) (rect_PQRS.height * rect_PQRS.width);
        if (rect_KLMN.intersects(rect_PQRS)) {
            Rectangle rect_int = rect_KLMN.intersection(rect_PQRS);
            areaInt_ = (int) (rect_int.height * rect_int.width);
        areaT_ = area1_ + area2_ - areaInt_;
        return areaT_;

Log in to reply

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