My C++ mistaken-at-first solution


  • 0
    K

    I thought it was to compute the overlap area of the two rectangles at first...And after I sketch on my paper for some time,I got my code like this:

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

    And I summit the code,got the "WA".
    I checked the last test case and read the question again, found it was to computer the total area.
    So I kept the function I wrote above,adding another function to call it,like this:
    (The total area equals to the sum of two rectangles minus the overlap area)

    int computeTheTrueArea(int A, int B, int C, int D, int E, int F, int G, int H) {
         return (C - A) * (D - B) + (G - E) * (H - F) - computeDupArea(A,B,C,D,E,F,G,H);
    }

Log in to reply
 

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