Four situation to solve this program.Beats 58.89% of cppsubmissions.


  • 0
    L

    class Solution {
    public:
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
    int x=0,y=0;
    int area1 = (C-A) (D-B);
    int area2 = (G-E)
    (H-F);
    //judge whether they overlap on Y-line
    if (G<=A ||E >=C)
    return area1+area2;
    //judge whether they overlap on X-line
    if(H<=B ||F >=D)
    return area1+area2;
    //four solution
    //one : AC include EG
    if(A<=E && C>=G){
    x = G-E;
    goto Y;
    }
    //two : G>=C &&E>=A
    if(G>=C && E>=A){
    x = C-E;
    goto Y;
    }
    //three: c>=G && A>=E
    if(C>=G && A>=E){
    x = G-A;
    goto Y;
    }
    //four: EG include AC
    if(E<=A && G>=C){
    x = C-A;
    goto Y;
    }
    Y:
    // the same as Y
    //four solution
    //one : BD include FH
    if(B<=F && D>=H){
    y = H-F;
    return area1+area2-xy;
    }
    //two : H>=D &&F>=B
    if(H>=D && F>=B){
    y = D-F;
    return area1+area2-x
    y;
    }
    //three: D>=H && B>=F
    if(D>=H && B>=F){
    y = H-B;
    return area1+area2-xy;
    }
    //four: FH include BD
    if(F<=B && H>=D){
    y = D-B;
    return area1+area2-x
    y;
    }
    return -1;
    }
    };


Log in to reply
 

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