-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcomputeArea.go
More file actions
38 lines (29 loc) · 882 Bytes
/
Copy pathcomputeArea.go
File metadata and controls
38 lines (29 loc) · 882 Bytes
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
/* https://leetcode.com/problems/rectangle-area/description/
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.
https://leetcode.com/static/images/problemset/rectangle_area.png
Assume that the total area is never beyond the maximum possible value of int.
*/
package lmath
func computeArea(A int, B int, C int, D int, E int, F int, G int, H int) int {
max := func(a, b int) int {
if a > b {
return a
}
return b
}
min := func(a, b int) int {
if a < b {
return a
}
return b
}
edge := func(A, C int) int {
// 相交或者不相交的情况
return max(0, C-A)
}
area := func(A, B, C, D int) int {
return edge(A, C) * edge(B, D)
}
return area(A, B, C, D) + area(E, F, G, H) - area(max(A, E), max(B, F), min(C, G), min(D, H))
}