A 20ms c solution using several lines


  • 0
    L

    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {

    if(G<=A||E>=C||D<=F||B>=H){
        return (C-A)*(D-B)+(G-E)*(H-F);
    }
    if(A==C||B==D){
        return (G-E)*(H-F);
    }
    if(E==G||H==F){
        return (C-A)*(D-B);
    }
    
    int tot=(C-A)*(D-B)+(G-E)*(H-F);
    int coverwidth=0;
    int coverheight=0;
    coverwidth=(G>C?C:G)-(E>A?E:A);
    coverheight=(D>H?H:D)-(B>F?B:F);
    
    return tot-coverwidth*coverheight;
    

    }

    But I don't know how to make it faster. Does any one know how to speed it up?


  • 0
    L

    A shorter solution, but the same speed:

    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {

    int tot=(C-A)*(D-B)+(G-E)*(H-F);
    
    if(G<=A||E>=C||D<=F||B>=H){
        return tot;
    }
    
    return tot-((G>C?C:G)-(E>A?E:A))*((D>H?H:D)-(B>F?B:F));
    

    }


Log in to reply
 

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