C++_Accepted_brief explanation


  • 2

    This question is a little bit of tricky...

    class Solution {
    public:
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        int area1 = (C-A)*(D-B);
        int area2 = (G-E)*(H-F);
        //1:(A,D),(C,D),(C,B),(A,B)
        //2:(E,H),(G,H),(G,F),(E,F)
        
        if(A>=G || E>=C || B>=H || F>=D){return area1 + area2;}
        //one top >= another bottom, one left >= another right
        return area1 + area2 - (min(C,G) - max(A,E))*(min(D,H) - max(B,F));
        // min left - max right, min up - max bottom.
        
    }
    

    };


Log in to reply
 

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