If you want to laugh, look at my solution

• ``````This is utterly ridiculous. As I was writing this I knew I was going about it wrong but I wanted to finish it before I thought of a different method or looked at any other solutions.

int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int area1 = (D - B)*(C - A);
int area2 = (H - F)*(G - E);
int area3;
if (area1 == 0) {
return area2;
}
if (area2 == 0) {
return area1;
}
if ((A == D) && (B == F) && (C == G) && (D == H)) {
return area1;
}
if ((E >= C) | (G <= A) | (H <= B) | (D <= F)) {    //not overlapping
return (area1 + area2);
}
if (((G - E) <= (C - A)) && ((H - F) <= (D - B)) && (E >= A) && (F >= B) && (G <= C) && (D >= H)) {                        //rect2 is inside rect1
return area1;
}
if (((C - A) <= (G - E)) && ((D - B) <= (H - F)) && (E <= A) && (B >= F) && (G >= C) && (H >= D)) {                        //rect1 is inside rect2
return area2;
}
if ((F >= B) && (E >= A) && (G >= C) && (H >= D)) {                       //overlapping upper right corner
area3 = (C - E)*(D - F);
}
else if ((F >= B) && (E <= A) && (G <= C) && (H >= D)) {                       //overlapping upper left corner
area3 = (G - A)*(D - F);
}
else if ((F <= B) && (E <= A) && (G <= C) && (H <= D)) {                       //overlapping bottom left corner
area3 = (G - A)*(H - B);
}
else if ((F <= B) && (E >= A) && (G >= C) && (H <= D)) {                        //overlapping bottom right corner
area3 = (H - B)*(C - E);
}
else if (((C - A) <= (G - E)) && (H <= D) && (G >= C) && (E <= A) && (F <= B)) {               //overlapping bottom side
area3 = (C - A)*(H - B);
}
else if (((C - A) <= (G - E)) && (H >= D) && (G >= C) && (E <= A) && (F >= B)) {               //overlapping top side
area3 = (C - A)*(D - F);
}
else if (((D - B) <= (H - F)) && (E <= A) && (F <= B) && (H >= D) && (G <= C)) {               //overlapping left side
area3 = (G - A)*(D - B);
}
else if (((D - B) <= (H - F)) && (E >= A) && (F <= B) && (H >= D) && (G >= C)) {               //overlapping right side
area3 = (C - E)*(D - B);
}
else if (((C - A) >= (G - E)) && (E >= A) && (F >= B) && (C >= G) && (D <= H)) {      //overlapping part of top side
area3 = (G - E)*(D - F);
}
else if (((C - A) >= (G - E)) && (A <= E) && (B >= F) && (G <= C) && (D >= H)) {       //overlapping part of bottom side
area3 = (G - E)*(H - B);
}
else if (((D - B) >= (H - F)) && (E <= A) && (F >= B) && (G <= C) && (H <= D)) {      //overlapping part of left side
area3 = (G - A)*(H - F);
}
else if (((D - B) >= (H - F)) && (E >= A) && (F >= B) && (G >= C) && (H <= D)) {       //overlapping part of right side
area3 = (C - E)*(H - F);
}
else if (((G - E) <= (C - A)) && (E >= A) && (F <= B) && (G <= C) && (H >= D)) {     //overlapping top and bottom
area3 = (G - E)*(D - B);
}
else if (((H - F) <= (D - B)) && (E <= A) && (F >= B) && (C <= G) && (D >= H)) {     //overlapping left and right
area3 = (C - A)*(H - F);
}

return (area1 + area2 - area3);
}``````

• HAHAH

You should only consider this problem in one dimension, and multiply them.

``````int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int area1 = (C-A)*(D-B), area2 = (G-E)*(H-F);
int coverX = C < E ? 0 : C < G ? A < E ? C-E : C-A : A > G ? 0 : A < E ? G-E : G-A;
int coverY = D < F ? 0 : D < H ? B < F ? D-F : D-B : B > H ? 0 : B < F ? H-F : H-B;
return area1 + area2 - coverX * coverY;
}``````

• Holy ****... I'm quite impressed that you got that right. I would make so many mistakes :-)

• HAHAHA
I'm here to laugh

• You guys are so patient (:зゝ∠)

• I was trying to do the same thing... But lucky for me, I gave up halfway...

• it kind of made my day.

• I was trying to do the same thing too.....

• Yeah it took me several hours lol.

• this is the most beautiful program i've ever seen in my life, lol

• The feedback of wrong test case also makes me to take every overlap condition into account
too .....

• This is crazy....

• It is not that hard if you use some copy and paste skill. Still it is troublesome though

• It's an interesting process to find out all the situation.

• it turned out that I'm not the only one stubborn here :D. I did it for the same exact reason as you did:

This is utterly ridiculous. As I was writing this I knew I was going about it wrong but I wanted to finish it before I thought of a different method or looked at any other solutions.

``````public class Solution {
public boolean insideAnotherRect(int a, int b, int c, int d, int x, int y) {
if (x >= a && x <= c && y >= b && y <= d) {
return true;
}
return false;
}

public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int intersection = 0;
int rect1Area = Math.abs(G-E)*Math.abs(H-F);
int npairsR1 = 0;
int[][] pairsRect1 = new int[4][2];
int rect2Area = Math.abs(C-A)*Math.abs(D-B);
int npairsR2 = 0;
int[][] pairsRect2 = new int[4][2];
if (this.insideAnotherRect(A, B, C, D, E, F)) {
pairsRect1[npairsR1][0] = E;
pairsRect1[npairsR1++][1] = F;
}
if (this.insideAnotherRect(A, B, C, D, E, H)) {
pairsRect1[npairsR1][0] = E;
pairsRect1[npairsR1++][1] = H;
}
if (this.insideAnotherRect(A, B, C, D, G, F)) {
pairsRect1[npairsR1][0] = G;
pairsRect1[npairsR1++][1] = F;
}
if (this.insideAnotherRect(A, B, C, D, G, H)) {
pairsRect1[npairsR1][0] = G;
pairsRect1[npairsR1++][1] = H;
}
if (this.insideAnotherRect(E, F, G, H, A, B)) {
pairsRect2[npairsR2][0] = A;
pairsRect2[npairsR2++][1] = B;
}
if (this.insideAnotherRect(E, F, G, H, A, D)) {
pairsRect2[npairsR2][0] = A;
pairsRect2[npairsR2++][1] = D;
}
if (this.insideAnotherRect(E, F, G, H, C, B)) {
pairsRect2[npairsR2][0] = C;
pairsRect2[npairsR2++][1] = B;
}
if (this.insideAnotherRect(E, F, G, H, C, D)) {
pairsRect2[npairsR2][0] = C;
pairsRect2[npairsR2++][1] = D;
}
if (npairsR1 == 4) {
intersection = rect1Area;
} else if (npairsR2 == 4) {
intersection = rect2Area;
} else if (npairsR1 == 2) {
if (pairsRect1[0][0] == pairsRect1[1][0]) {
if (pairsRect1[0][0] == E) {
intersection = Math.abs(C-E)*Math.abs(H-F);
} else {
intersection = Math.abs(A-G)*Math.abs(H-F);
}
} else if (pairsRect1[0][1] == pairsRect1[1][1]) {
if (pairsRect1[0][1] == H) {
intersection = Math.abs(G-E)*Math.abs(H-B);
} else {
intersection = Math.abs(G-E)*Math.abs(D-F);
}
}
} else if (npairsR2 == 2) {
if (pairsRect2[0][0] == pairsRect2[1][0]) {
if (pairsRect2[0][0] == A) {
intersection = Math.abs(G-A)*Math.abs(D-B);
} else {
intersection = Math.abs(E-C)*Math.abs(D-B);
}
} else if (pairsRect2[0][1] == pairsRect2[1][1]) {
if (pairsRect2[0][1] == D) {
intersection = Math.abs(C-A)*Math.abs(D-F);
} else {
intersection = Math.abs(C-A)*Math.abs(H-B);
}
}
} else if (npairsR1 == 1 && npairsR2 == 1) {
intersection = Math.abs(pairsRect1[0][0]-pairsRect2[0][0])*Math.abs(pairsRect1[0][1]-pairsRect2[0][1]);
} else if (npairsR1 == 0 && npairsR2 == 0) {
if (this.insideAnotherRect(E, F, G, H, E, B) && this.insideAnotherRect(E, F, G, H, G, D) && this.insideAnotherRect(A, B, C, D, E, B) && this.insideAnotherRect(A, B, C, D, G, D)) {
intersection = Math.abs(E-G)*Math.abs(D-B);
} else if (this.insideAnotherRect(E, F, G, H, A, F) && this.insideAnotherRect(E, F, G, H, C, H) && this.insideAnotherRect(A, B, C, D, A, F) && this.insideAnotherRect(A, B, C, D, C, H)) {
intersection = Math.abs(A-C)*Math.abs(F-H);
}
}
return rect2Area + rect1Area - intersection;
}
}``````

• I'm here to laugh LOL

• LOL, you won.:laughing:

• You won,dude !! LOL.

• Haha...I was trying to do the same thing...

• Just pulled out my wisdom teeth and could not laugh loud

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