-
Notifications
You must be signed in to change notification settings - Fork 2
/
0391.perfect-rectangle.go
59 lines (52 loc) · 1.02 KB
/
0391.perfect-rectangle.go
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
package _0391
import (
"math"
)
func isRectangleCover(rectangles [][]int) bool {
pos1 := math.MaxInt32
pos2 := math.MaxInt32
pos3 := math.MinInt32
pos4 := math.MinInt32
area := 0
set := map[int]bool{}
for _, rect := range rectangles {
pos1 = min(pos1, rect[0])
pos2 = min(pos2, rect[1])
pos3 = max(pos3, rect[2])
pos4 = max(pos4, rect[3])
area += (rect[2] - rect[0]) * (rect[3] - rect[1])
setPut(set, rect[0], rect[1])
setPut(set, rect[0], rect[3])
setPut(set, rect[2], rect[3])
setPut(set, rect[2], rect[1])
}
if (pos3-pos1)*(pos4-pos2) != area {
return false
}
if len(set) != 4 {
return false
}
if set[pos1*10000000+pos2] && set[pos1*10000000+pos4] && set[pos3*10000000+pos4] && set[pos3*10000000+pos2] {
return true
}
return false
}
func setPut(set map[int]bool, x, y int) {
if set[x*10000000+y] {
delete(set, x*10000000+y)
} else {
set[x*10000000+y] = true
}
}
func min(a, b int) int {
if a > b {
return b
}
return a
}
func max(a, b int) int {
if b > a {
return b
}
return a
}