|
| 1 | +# [391. Perfect Rectangle](https://leetcode-cn.com/problems/perfect-rectangle/) |
| 2 | + |
| 3 | +## 题目 |
| 4 | + |
| 5 | +Given an array rectangles where rectangles[i] = [xi, yi, ai, bi] represents an axis-aligned rectangle. The bottom-left point of the rectangle is (xi, yi) and the top-right point of it is (ai, bi). |
| 6 | + |
| 7 | +Return true if all the rectangles together form an exact cover of a rectangular region. |
| 8 | + |
| 9 | +**Example1:** |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | + Input: rectangles = [[1,1,3,3],[3,1,4,2],[3,2,4,4],[1,3,2,4],[2,3,3,4]] |
| 14 | + Output: true |
| 15 | + Explanation: All 5 rectangles together form an exact cover of a rectangular region. |
| 16 | + |
| 17 | +**Example2:** |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + Input: rectangles = [[1,1,2,3],[1,3,2,4],[3,1,4,2],[3,2,4,4]] |
| 22 | + Output: false |
| 23 | + Explanation: Because there is a gap between the two rectangular regions. |
| 24 | + |
| 25 | +**Example3:** |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + Input: rectangles = [[1,1,3,3],[3,1,4,2],[1,3,2,4],[3,2,4,4]] |
| 30 | + Output: false |
| 31 | + Explanation: Because there is a gap in the top center. |
| 32 | + |
| 33 | +**Example4:** |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | + Input: rectangles = [[1,1,3,3],[3,1,4,2],[1,3,2,4],[2,2,4,4]] |
| 38 | + Output: false |
| 39 | + Explanation: Because two of the rectangles overlap with each other. |
| 40 | + |
| 41 | +**Constraints:** |
| 42 | + |
| 43 | +- 1 <= rectangles.length <= 2 * 10000 |
| 44 | +- rectangles[i].length == 4 |
| 45 | +- -100000 <= xi, yi, ai, bi <= 100000 |
| 46 | + |
| 47 | +## 题目大意 |
| 48 | + |
| 49 | +给你一个数组 rectangles ,其中 rectangles[i] = [xi, yi, ai, bi] 表示一个坐标轴平行的矩形。这个矩形的左下顶点是 (xi, yi) ,右上顶点是 (ai, bi) 。 |
| 50 | + |
| 51 | +如果所有矩形一起精确覆盖了某个矩形区域,则返回 true ;否则,返回 false 。 |
| 52 | + |
| 53 | +## 解题思路 |
| 54 | + |
| 55 | +- 矩形区域的面积等于所有矩形的面积之和并且满足矩形区域四角的顶点只能出现一次,且其余顶点的出现次数只能是两次或四次则返回true,否则返回false |
| 56 | + |
| 57 | +## 代码 |
| 58 | + |
| 59 | +```go |
| 60 | +package leetcode |
| 61 | + |
| 62 | +type point struct { |
| 63 | + x int |
| 64 | + y int |
| 65 | +} |
| 66 | + |
| 67 | +func isRectangleCover(rectangles [][]int) bool { |
| 68 | + minX, minY, maxA, maxB := rectangles[0][0], rectangles[0][1], rectangles[0][2], rectangles[0][3] |
| 69 | + area := 0 |
| 70 | + cnt := make(map[point]int) |
| 71 | + for _, v := range rectangles { |
| 72 | + x, y, a, b := v[0], v[1], v[2], v[3] |
| 73 | + area += (a - x) * (b - y) |
| 74 | + minX = min(minX, x) |
| 75 | + minY = min(minY, y) |
| 76 | + maxA = max(maxA, a) |
| 77 | + maxB = max(maxB, b) |
| 78 | + cnt[point{x, y}]++ |
| 79 | + cnt[point{a, b}]++ |
| 80 | + cnt[point{x, b}]++ |
| 81 | + cnt[point{a, y}]++ |
| 82 | + } |
| 83 | + if area != (maxA - minX) * (maxB - minY) || |
| 84 | + cnt[point{minX, minY}] != 1 || cnt[point{maxA, maxB}] != 1 || |
| 85 | + cnt[point{minX, maxB}] != 1 || cnt[point{maxA, minY}] != 1 { |
| 86 | + return false |
| 87 | + } |
| 88 | + delete(cnt, point{minX, minY}) |
| 89 | + delete(cnt, point{maxA, maxB}) |
| 90 | + delete(cnt, point{minX, maxB}) |
| 91 | + delete(cnt, point{maxA, minY}) |
| 92 | + for _, v := range cnt { |
| 93 | + if v != 2 && v != 4 { |
| 94 | + return false |
| 95 | + } |
| 96 | + } |
| 97 | + return true |
| 98 | +} |
| 99 | + |
| 100 | +func min(a, b int) int { |
| 101 | + if a < b { |
| 102 | + return a |
| 103 | + } |
| 104 | + return b |
| 105 | +} |
| 106 | + |
| 107 | +func max(a, b int) int { |
| 108 | + if a > b { |
| 109 | + return a |
| 110 | + } |
| 111 | + return b |
| 112 | +} |
| 113 | +``` |
0 commit comments