simply sum the two rectangle areas, minus the overlap area(10 lines java)


  • -1
    A

    public class Solution {
    public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
    int areaABCD = (C-A)(D-B);
    int areaEFGH = (G-E)
    (H-F);
    if(H<=B || F>=D || A>=G || C<=E) return areaABCD+areaEFGH;
    int height = D>=H? (F<=B?(H-B):(H-F)) : (F>=B?(D-F):(D-B));
    int width = A>=E? (G<=C?(G-A):(C-A)) : (C<=G?(C-E):(G-E));
    return areaABCD+areaEFGH-height*width;
    }
    }


Log in to reply
 

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