# 1 解题思想

 核心思想就是:能够正好围成一个矩形的情况就是:

- 最左下 最左上 最右下 最右上 的四个点只出现过一次,其他肯定是成对出现的(保证完全覆盖)
- 上面四个点围成的面积,正好等于所有子矩形的面积之和(保证不重复)

# 2 原题

Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover of a rectangular region.

Each rectangle is represented as a bottom-left point and a top-right point. For example, a unit square is represented as [1,1,2,2]. (coordinate of bottom-left point is (1, 1) and top-right point is (2, 2)).

Example 1:

rectangles = [
[1,1,3,3],
[3,1,4,2],
[3,2,4,4],
[1,3,2,4],
[2,3,3,4]
]

Return true. All 5 rectangles together form an exact cover of a rectangular region.

Example 2:

rectangles = [
[1,1,2,3],
[1,3,2,4],
[3,1,4,2],
[3,2,4,4]
]

Return false. Because there is a gap between the two rectangular regions.

Example 3:

rectangles = [
[1,1,3,3],
[3,1,4,2],
[1,3,2,4],
[3,2,4,4]
]

Return false. Because there is a gap in the top center.

Example 4:

rectangles = [
[1,1,3,3],
[3,1,4,2],
[1,3,2,4],
[2,2,4,4]
]

Return false. Because two of the rectangles overlap with each other.


# 3 AC解

/**
* 核心思想就是:能够正好围成一个矩形的情况就是:
* 有且只有:
*   - 最左下 最左上 最右下 最右上 的四个点只出现过一次,其他肯定是成对出现的(保证完全覆盖)
*   - 上面四个点围成的面积,正好等于所有子矩形的面积之和(保证不重复)
* Created by MebiuW on 16/8/29.
*/
public class Solution {
public boolean isRectangleCover(int[][] rectangles) {
int left = Integer.MAX_VALUE;
int right = Integer.MIN_VALUE;
int top = Integer.MIN_VALUE;
int bottom = Integer.MAX_VALUE;
int n = rectangles.length;
HashSet<String> flags = new HashSet<String>();
int totalArea = 0;
for(int i=0;i<n;i++){
left = Math.min(left,rectangles[i][0]);
bottom = Math.min(bottom,rectangles[i][1]);
right = Math.max(right,rectangles[i][2]);
top = Math.max(top,rectangles[i][3]);
totalArea += (rectangles[i][3]-rectangles[i][1])*(rectangles[i][2]-rectangles[i][0]);
String pointLT = rectangles[i][0] + " "+ rectangles[i][3];
String pointLB = rectangles[i][0] + " "+ rectangles[i][1];
String pointRT = rectangles[i][2] + " "+ rectangles[i][3];
String pointRB = rectangles[i][2] + " "+ rectangles[i][1];
}
if(flags.size()==4 && flags.contains(left+" "+top) && flags.contains(left+" "+bottom) && flags.contains(right+" "+bottom) && flags.contains(right+" "+top)){
}
return false;
}
}

#### [leetcode] 391. Perfect Rectangle 解题报告

2016-09-09 05:33:29

#### 391. Perfect Rectangle

2016-09-09 23:14:26

#### Leetcode 391. Perfect Rectangle

2016-08-29 22:17:34

#### 391-完美的矩形

2018-04-12 20:18:24

#### LeetCode 85. Maximal Rectangle(最大子矩阵)

2016-09-04 19:48:32

#### [LeetCode] 01矩阵中最大矩形 Maximal Rectangle

2012-10-14 00:10:00

#### Dialog 完美诠释

2014-08-27 09:20:43

#### 最大长方体问题

2013-06-12 13:44:48

#### 任意连通图的哈密顿回路计算流程

2015-06-20 14:04:08

#### 精于硬件，臻于完美

2012-11-14 09:00:12

## 不良信息举报

Leetcode 391. Perfect Rectangle 完美矩形 解题报告