[LeetCode] Perfect Rectangle 完美矩形
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.
这道题是LeetCode第二周编程比赛的压轴题目,然而我并没有做出来,我想了两种方法都无法通过OJ的大数据集合,第一种方法是对于每一个矩形,我将其拆分为多个面积为1的单位矩形,然后以其左下方的点为标记,用一个哈希表建立每一个单位矩形和遍历到的矩形的映射,因为每个单位矩形只能属于一个矩形,否则就会有重叠,我感觉这种思路应该没错,但是由于把每一个遍历到的矩形拆分为单位矩形再建立映射很费时间,尤其是当矩形很大的时候,TLE就很正常了,后来我试的第二种方法是对于遍历到的每个矩形都和其他所有矩形检测一遍是否重叠,这种方法也是毫无悬念的TLE。
博主能力有限,只能去论坛中找各位大神的解法,发现下面两种方法比较fancy,也比较好理解。首先来看第一种方法,这种方法的设计思路很巧妙,利用了mask,也就是位操作Bit Manipulation的一些技巧,下面这张图来自这个帖子:
所有的矩形的四个顶点只会有下面蓝,绿,红三种情况,其中蓝表示该顶点周围没有其他矩形,T型的绿点表示两个矩形并排相邻,红点表示四个矩形相邻,那么在一个完美矩形中,蓝色的点只能有四个,这是个很重要的判断条件。我们再来看矩形的四个顶点,我们按照左下,左上,右上,右下的顺序来给顶点标号为1,2,4,8,为啥不是1,2,3,4呢,我们注意它们的二进制1(0001),2(0010),4(0100),8(1000),这样便于我们与和或的操作,我们还需要知道的一个判定条件是,当一个点是某一个矩形的左下顶点时,这个点就不能是其他矩形的左下顶点了,这个条件对于这四种顶点都要成立,那么对于每一个点,如果它是某个矩形的四个顶点之一,我们记录下来,如果在别的矩形中它又是相同的顶点,那么直接返回false即可,这样就体现了我们标记为1,2,4,8的好处,我们可以按位检查1。如果每个点的属性没有冲突,那么我们来验证每个点的mask是否合理,通过上面的分析,我们知道每个点只能是蓝,绿,红三种情况的一种,其中蓝的情况是mask的四位中只有一个1,分别就是1(0001),2(0010),4(0100),8(1000),而且蓝点只能有四个;那么对于T型的绿点,mask的四位中有两个1,那么就有六种情况,分别是12(1100), 10(1010), 9(1001), 6(0110), 5(0101), 3(0011);而对于红点,mask的四位都是1,只有一种情况15(1111),那么我们可以通过直接找mask是1,2,4,8的个数,也可以间接通过找不是绿点和红点的个数,看是否是四个。最后一个判定条件是每个矩形面积累加和要等于最后的大矩形的面积,那么大矩形的面积我们通过计算最小左下点和最大右上点来计算出来即可, 参见代码如下:
解法一:
class Solution {
public:
bool isRectangleCover(vector<vector<int>>& rectangles) {
unordered_map<string, int> m;
int min_x = INT_MAX, min_y = INT_MAX, max_x = INT_MIN, max_y = INT_MIN, area = , cnt = ;
for (auto rect : rectangles) {
min_x = min(min_x, rect[]);
min_y = min(min_y, rect[]);
max_x = max(max_x, rect[]);
max_y = max(max_y, rect[]);
area += (rect[] - rect[]) * (rect[] - rect[]);
if (!isValid(m, to_string(rect[]) + "_" + to_string(rect[]), )) return false; // bottom-left
if (!isValid(m, to_string(rect[]) + "_" + to_string(rect[]), )) return false; // top-left
if (!isValid(m, to_string(rect[]) + "_" + to_string(rect[]), )) return false; // top-right
if (!isValid(m, to_string(rect[]) + "_" + to_string(rect[]), )) return false; // bottom-right
}
for (auto it = m.begin(); it != m.end(); ++it) {
int t = it->second;
if (t != && t != && t != && t != && t != && t != && t!= ) {
++cnt;
}
}
return cnt == && area == (max_x - min_x) * (max_y - min_y);
}
bool isValid(unordered_map<string, int>& m, string corner, int type) {
int& val = m[corner];
if (val & type) return false;
val |= type;
return true;
}
};
下面这种方法也相当的巧妙, 提出这种算法的大神细心的发现了每种点的规律,每个绿点其实都是两个顶点的重合,每个红点都是四个顶点的重合,而每个蓝点只有一个顶点,有了这条神奇的性质就不用再去判断“每个点最多只能是一个矩形的左下,左上,右上,或右下顶点”这条性质了,我们直接用一个set,对于遍历到的任意一个顶点,如果set中已经存在了,则删去这个点,如果没有就加上,这样最后会把绿点和红点都滤去,剩下的都是蓝点,我们只要看蓝点的个数是否为四个,再加上检测每个矩形面积累加和要等于最后的大矩形的面积即可,参见代码如下:
解法二:
class Solution {
public:
bool isRectangleCover(vector<vector<int>>& rectangles) {
unordered_set<string> st;
int min_x = INT_MAX, min_y = INT_MAX, max_x = INT_MIN, max_y = INT_MIN, area = ;
for (auto rect : rectangles) {
min_x = min(min_x, rect[]);
min_y = min(min_y, rect[]);
max_x = max(max_x, rect[]);
max_y = max(max_y, rect[]);
area += (rect[] - rect[]) * (rect[] - rect[]);
string s1 = to_string(rect[]) + "_" + to_string(rect[]); // bottom-left
string s2 = to_string(rect[]) + "_" + to_string(rect[]); // top-left
string s3 = to_string(rect[]) + "_" + to_string(rect[]); // top-right
string s4 = to_string(rect[]) + "_" + to_string(rect[]); // bottom-right
if (st.count(s1)) st.erase(s1);
else st.insert(s1);
if (st.count(s2)) st.erase(s2);
else st.insert(s2);
if (st.count(s3)) st.erase(s3);
else st.insert(s3);
if (st.count(s4)) st.erase(s4);
else st.insert(s4);
}
string t1 = to_string(min_x) + "_" + to_string(min_y);
string t2 = to_string(min_x) + "_" + to_string(max_y);
string t3 = to_string(max_x) + "_" + to_string(max_y);
string t4 = to_string(max_x) + "_" + to_string(min_y);
if (!st.count(t1) || !st.count(t2) || !st.count(t3) || !st.count(t4) || st.size() != ) return false;
return area == (max_x - min_x) * (max_y - min_y);
}
};
参考资料:
https://discuss.leetcode.com/topic/56052/really-easy-understanding-solution-o-n-java
https://discuss.leetcode.com/topic/55997/short-java-solution-with-explanation-updated/2
https://discuss.leetcode.com/topic/55923/o-n-solution-by-counting-corners-with-detailed-explaination
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Perfect Rectangle 完美矩形的更多相关文章
- 391 Perfect Rectangle 完美矩形
有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域.每个矩形用左下角的点和右上角的点的坐标来表示.例如, 一个单位正方形可以表示为 [1,1,2,2]. ( ...
- Perfect Rectangle(完美矩形)
我们有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域. 每个矩形用左下角的点和右上角的点的坐标来表示.例如, 一个单位正方形可以表示为 [1,1,2,2] ...
- [LeetCode] Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- Leetcode: Perfect Rectangle
Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...
- [LeetCode] Perfect Number 完美数字
We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...
- [LeetCode] 223. Rectangle Area 矩形面积
Find the total area covered by two rectilinearrectangles in a 2D plane. Each rectangle is defined by ...
- [LeetCode]223. Rectangle Area矩形面积
/* 像是一道数据分析题 思路就是两个矩形面积之和减去叠加面积之和 */ public int computeArea(int A, int B, int C, int D, int E, int F ...
- Leetcode 391.完美矩形
完美矩形 我们有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域. 每个矩形用左下角的点和右上角的点的坐标来表示.例如, 一个单位正方形可以表示为 [1,1 ...
- Java实现 LeetCode 391 完美矩形
391. 完美矩形 我们有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域. 每个矩形用左下角的点和右上角的点的坐标来表示.例如, 一个单位正方形可以表示为 ...
随机推荐
- 移动端web开发的那些坑
1.为非a列表项添加触感样式 通过js注册touchstart和touchend事件,添加触感class的方式, 有个坑,低版本的Android浏览器,经常触发不到touchend,需要再额外注册一个 ...
- MVC5学习系列--Razor视图(一)
前言 嗷~小弟我又出现了~咳咳..嚎过头了, 先说一说为什么写这个吧,~首先肯定是我自己需要学(废话 - -,)//,之前也写过MVC4的项目,嗯..但是仅限于使用并没有很深入的每个模块去了解, 这段 ...
- [Spring]04_最小化Spring XML配置
4.1 自动装配 Bean Spring 装配 bean 时,有时非常明确,就是需要将某个 bean 的引用装配给指定属性. 例如,若应用上下文中只有一个 javax.sql.DataSource 类 ...
- iOS 编辑UITableView(根据iOS编程编写)
上个项目我们完成了 JXHomepwner 简单的应用展示,项目地址.本节我们需要在上节项目基础上,增加一些响应用户操作.包括添加,删除和移动表格. 编辑模式 UITableView 有一个名为 e ...
- SQL Server 中怎么查看一个字母的ascii编码或者Unicode编码
参考文章:微信公众号文章 在sql中怎么查看一个字符的ascii编码,so easy !! select ASCII('a') SELECT CHAR(97) charNum SELECT UNICO ...
- [python] File path and system path
1. get files in the current directory with the assum that the directory is like this: a .py |----dat ...
- macOS 升级到了10.12.1
除了明面上的一些更新,但我感觉最重要的是触摸板的行为特征又还原了.
- PHP unserialize()
定义和用法 unserialize() 将已序列化的字符串还原回 PHP 的值. 序列化请使用 serialize() 函数. 语法 unserialize(str) 参数 描述 str 必需.一个序 ...
- Java基础知识笔记(五:多线程的同步问题)
编写多线程程序往往是为了提高资源的利用率,或者提高程序的运行效率,或者更好地监控程序的运行过程等.多线程同步处理的目的是为了让多个线程协调地并发工作.对多线程进行同步处理可以通过同步方法和同步语句块实 ...
- 分布式文件系统 - FastDFS 配置 Nginx 模块及上传测试
也不说废话,直接干 上一篇 分布式文件系统 - FastDFS 在 CentOS 下配置安装部署 中安装了 FastDFS 后,并配置启动了 Tracker 和 Storage 服务,已经可以上传文件 ...