12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- type pair struct {
- _1 int
- _2 int
- }
- func isRectangleCover(rectangles [][]int) bool {
- w, a, s, d := rectangles[0][3], rectangles[0][0], rectangles[0][1], rectangles[0][2]
- area := 0
- m := make(map[pair]bool)
- for i := range rectangles {
- cw, ca, cs, cd := rectangles[i][3], rectangles[i][0], rectangles[i][1], rectangles[i][2]
- w, a, s, d = maxInt(w, cw), minInt(a, ca), minInt(s, cs), maxInt(d, cd) // Record the max rectangle
- area += (cw - cs) * (cd - ca)
- put(&m, ca, cw) // If the coverage is perfect, all points appear in pair except tl, tr, bl and br.
- put(&m, cd, cw)
- put(&m, ca, cs)
- put(&m, cd, cs)
- }
- return m[pair{a, w}] && m[pair{d, w}] && m[pair{a, s}] && m[pair{d, s}] && len(m) == 4 && area == (w-s)*(d-a)
- }
- func put(m *map[pair]bool, x, y int) {
- p := pair{x, y}
- if _, ok := (*m)[p]; ok {
- delete(*m, p)
- } else {
- (*m)[p] = true
- }
- }
- func maxInt(x, y int) int {
- if x < y {
- return y
- }
- return x
- }
- func minInt(x, y int) int {
- if x < y {
- return x
- }
- return y
- }
|