223. Rectangle Area

  • 32.3%

https://leetcode.com/problems/rectangle-area/

Find the total area covered by two rectilinear rectangles in a 2D plane.

Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.

image

Assume that the total area is never beyond the maximum possible value of int.


需要思考


cpp

solution 1:

36ms, 20.98%, July 16th, 2016

https://discuss.leetcode.com/topic/17534/if-you-want-to-laugh-look-at-my-solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class Solution {
public:
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);
}
};

python

solution 1:

136ms, 58.33%, 16 July 2016

https://discuss.leetcode.com/topic/22452/python-concise-solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution(object):
def computeArea(self, A, B, C, D, E, F, G, H):
"""
:type A: int
:type B: int
:type C: int
:type D: int
:type E: int
:type F: int
:type G: int
:type H: int
:rtype: int
"""
overlap = max(min(C, G) - max(A, E), 0) * max(min(D, H) - max(B, F), 0)
return (A - C) * (B - D) + (E - G) * (F - H) - overlap

java

solution 1:

5ms, 14.77%, 16 July 2016

https://discuss.leetcode.com/topic/15733/my-java-solution-sum-of-areas-overlapped-area

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Solution {
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int areaOfSqrA = (C - A) * (D - B);
int areaOfSqrB = (G - E) * (H - F);

int left = Math.max(A, E);
int right = Math.min(G, C);
int bottom = Math.max(F, B);
int top = Math.min(D, H);

int overlap = 0;
if(right > left && top > bottom)
overlap = (right - left) * (top - bottom);

return areaOfSqrA + areaOfSqrB - overlap;
}
}

solution 2:

6ms, 6.25%, 16 July 2016

https://discuss.leetcode.com/topic/17705/just-another-short-way

1
2
3
4
5
6
7
public class Solution {
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int left = Math.max(A, E), right = Math.max(Math.min(C, G), left);
int bottom = Math.max(B, F), top = Math.max(Math.min(D, H), bottom);
return (C - A) * (D - B) - (right - left) * (top - bottom) + (G - E) * (H - F);
}
}
谢谢你,可爱的朋友。