Perfect Rectangle(完美矩形)
我们有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域。
每个矩形用左下角的点和右上角的点的坐标来表示。例如, 一个单位正方形可以表示为 [1,1,2,2]。 ( 左下角的点的坐标为 (1, 1) 以及右上角的点的坐标为 (2, 2) )。

示例 1:
rectangles = [
[1,1,3,3],
[3,1,4,2],
[3,2,4,4],
[1,3,2,4],
[2,3,3,4]
] 返回 true。5个矩形一起可以精确地覆盖一个矩形区域。

示例 2:
rectangles = [
[1,1,2,3],
[1,3,2,4],
[3,1,4,2],
[3,2,4,4]
] 返回 false。两个矩形之间有间隔,无法覆盖成一个矩形。

示例 3:
rectangles = [
[1,1,3,3],
[3,1,4,2],
[1,3,2,4],
[3,2,4,4]
] 返回 false。图形顶端留有间隔,无法覆盖成一个矩形。

示例 4:
rectangles = [
[1,1,3,3],
[3,1,4,2],
[1,3,2,4],
[2,2,4,4]
] 返回 false。因为中间有相交区域,虽然形成了矩形,但不是精确覆盖。 这道题我在写前两个解法的时候还没ac通过,但是已经忍不住要写了。思路还是很清晰的,遍历矩形,算有没有重合,面积累加,最后看看总面积是不是最大范围的覆盖的矩形的面积。
第一遍暴力解法:
class Solution {
public:
bool Cover(vector<int>& a, vector<int>&b)
{
if (min(a[], a[]) >= max(b[], b[]) || max(a[], a[]) <= min(b[], b[])
|| min(a[], a[]) >= max(b[], b[]) || max(a[], a[]) <= min(b[], b[]))
{
return false;
}
return true;
}
bool isRectangleCover(vector<vector<int>>& rectangles) {
int minx, miny, maxx, maxy;
minx = miny = INT_MAX;
maxx = maxy = INT_MIN;
long long int sum = ;
for (int i = ; i < rectangles.size();i++)
{
for (int r = ; r < i;r++)
{
if (Cover(rectangles[i], rectangles[r]))
{
return false;
}
}
minx = min(minx, min(rectangles[i][], rectangles[i][]));
miny = min(miny, min(rectangles[i][], rectangles[i][]));
maxx = max(maxx, max(rectangles[i][], rectangles[i][]));
maxy = max(maxy, max(rectangles[i][], rectangles[i][]));
sum += abs((rectangles[i][] - rectangles[i][])*(rectangles[i][] - rectangles[i][]));
}
return sum == (maxx - minx)*(maxy - miny);
}
};
果不其然挂了,测试最后两组都是3w+的数据量
第二遍:四叉树解法
class QuadNode
{
public:
enum{
quad_1,//四个象限
quad_2,
quad_3,
quad_4,
quad_count,
};
vector<vector<int>>* data;
QuadNode* Children[quad_count];//孩子指针,数组大小为8
QuadNode* Parent;//父节点指针
typedef std::list<int> RecList;
typedef std::list<int>::iterator RecListIter;
RecList rectlist;//携带的参数 实体列表
int quad;//在父节点中的象限
int deep;//自己所在的层索引
int minx,miny;
int maxx,maxy;
QuadNode(vector<vector<int>>* data, int x1, int x2, int y1, int y2, int dp, int qd)
{
minx = x1;
maxx = x2;
miny = y1;
maxy = y2;
deep = dp;
quad = qd;
Parent = NULL;
this->data = data;
memset(Children, , sizeof(Children));
}
~QuadNode()
{
for (int i = ; i < quad_count; i++)
{
if (Children[i])
{
delete Children[i];
Children[i] = NULL;
}
}
rectlist.clear();
}
QuadNode* GetDeepest(int index)
{
if (deep > )
{
//4个孩子都要创建
for (int r = ; r < QuadNode::quad_count; r++)
{
if (!Children[r])
{
int ix = r == QuadNode::quad_1 || r == QuadNode::quad_3 ? minx : (minx + maxx) / ;
int ax = r == QuadNode::quad_1 || r == QuadNode::quad_3 ? (minx + maxx) / : maxx; int iy = r == QuadNode::quad_1 || r == QuadNode::quad_2 ? miny : (miny + maxy) / ;
int ay = r == QuadNode::quad_1 || r == QuadNode::quad_2 ? (miny + maxy) / : maxy;
QuadNode *node = new QuadNode(data, ix, ax, iy, ay, deep - , r);
node->Parent = this;
Children[r] = node;
}
if (Children[r]->CheckInRange(index))
{
return Children[r]->GetDeepest(index);
}
}
}
return this;
}
bool CheckInRange(int index)
{
if ((*data)[index][] >= minx && (*data)[index][] <= maxx && (*data)[index][] >= miny && (*data)[index][] <= maxy)
{
return true;
}
return false;
}
bool CheckCover(int index)
{
QuadNode* n = GetDeepest(index);
QuadNode* parent = n->Parent;
while (parent)
{
if (parent->CheckWithTrianglelist(index))
{
return true;
}
parent = parent->Parent;
} if (n->CollisionCheck(index))
return true;
n->rectlist.push_back(index);
return false;
}
bool CollisionCheck(int index)
{
return CheckWithTrianglelist(index) || CheckWithSubSpace(index);
}
bool CheckWithTrianglelist(int index)
{
RecListIter itr = rectlist.begin();
while (itr != rectlist.end())
{
int id = *itr;
if (Cover((*data)[id], (*data)[index]))
{
return true;
}
itr++;
}
return false;
} bool CheckWithSubSpace(int index)
{
bool collision = false;
for (int i = ; i < quad_count && Children[i]; i++)
{
int vec[] = { minx, miny, maxx, maxy };
vector<int> para(vec, vec + );
if (Cover((*data)[index], para))
{
collision |= Children[i]->CollisionCheck(index);
}
if (collision)
{
return true;
}
}
return false;
} bool Cover(vector<int>& a, vector<int>&b)
{
if (min(a[], a[]) >= max(b[], b[]) || max(a[], a[]) <= min(b[], b[])
|| min(a[], a[]) >= max(b[], b[]) || max(a[], a[]) <= min(b[], b[]))
{
return false;
}
return true;
}
};
class Solution {
public:
int GetMax2Power(int xmax, int ymax, int& lg)
{
int max = xmax;
if (ymax > max)
max = ymax;
if ((max & (max - )) == )
{
double L = log(max*1.0) / log(2.0);
lg = (int)L + ;
return max;
}
else
{
double L = log(max*1.0) / log(2.0);
lg = (int)L + ;
return (int)pow( * 1.0, lg - 1.0);
}
}
bool isRectangleCover(vector<vector<int>>& rectangles) {
int minx, miny, maxx, maxy;
minx = miny = INT_MAX;
maxx = maxy = INT_MIN;
long long int sum = ;
for (int i = ; i < rectangles.size(); i++)
{
minx = min(minx, min(rectangles[i][], rectangles[i][]));
miny = min(miny, min(rectangles[i][], rectangles[i][]));
maxx = max(maxx, max(rectangles[i][], rectangles[i][]));
maxy = max(maxy, max(rectangles[i][], rectangles[i][]));
}
int mx = max(abs(maxx), abs(minx));
int my = max(abs(maxy), abs(miny));
int range, lg;
range = GetMax2Power(mx, my, lg);
//四叉树
QuadNode* root = new QuadNode(&rectangles, -range, range, -range, range, lg, ); for (int i = ; i < rectangles.size();i++)
{
if (root->CheckCover(i))
{
return false;
}
sum += abs((rectangles[i][] - rectangles[i][])*(rectangles[i][] - rectangles[i][]));
}
delete root;
return sum == (maxx - minx)*(maxy - miny);
}
};
以为没什么问题了,跑一下又超时了,真是恶心的一p啊。拿测试数据跑一跑发现,几乎95%的数据都在边界上,四叉树无法往下细化。写个四叉树容易吗???
只能硬着头皮继续想了,果然没有想到。参考了下别人的歪门邪道,感觉前面写的东西都白瞎了。有时候解决问题,还得靠技巧。
解法三:所有的矩形顶点,有且只有四个边角是只出现一次,剩下的顶点要么两次,要么四次
long long int getHash(int x, int y)
{
long long int t = << ;
return x*t + y;
}
bool isRectangleCover(vector<vector<int>>& rectangles) {
int minx, miny, maxx, maxy;
minx = miny = INT_MAX;
maxx = maxy = INT_MIN;
long long int sum = ;
unordered_set<long long int> st;
for (int i = ; i < rectangles.size(); i++)
{
minx = min(minx, min(rectangles[i][], rectangles[i][]));
miny = min(miny, min(rectangles[i][], rectangles[i][]));
maxx = max(maxx, max(rectangles[i][], rectangles[i][]));
maxy = max(maxy, max(rectangles[i][], rectangles[i][]));
sum += abs((rectangles[i][] - rectangles[i][])*(rectangles[i][] - rectangles[i][]));
long long int lu = getHash(rectangles[i][], rectangles[i][]);
long long int ld = getHash(rectangles[i][], rectangles[i][]);
long long int ru = getHash(rectangles[i][], rectangles[i][]);
long long int rd = getHash(rectangles[i][], rectangles[i][]);
if (st.count(lu) == ) st.insert(lu);
else st.erase(lu);
if (st.count(ld) == ) st.insert(ld);
else st.erase(ld);
if (st.count(ru) == ) st.insert(ru);
else st.erase(ru);
if (st.count(rd) == ) st.insert(rd);
else st.erase(rd);
} return sum == (maxx - minx)*(maxy - miny) && st.size() ==
&& st.count(getHash(minx, miny)) ==
&& st.count(getHash(minx, maxy)) ==
&& st.count(getHash(maxx, miny)) ==
&& st.count(getHash(maxx, maxy)) == ;
}

吐血!
Perfect Rectangle(完美矩形)的更多相关文章
- [LeetCode] Perfect Rectangle 完美矩形
Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...
- 391 Perfect Rectangle 完美矩形
有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域.每个矩形用左下角的点和右上角的点的坐标来表示.例如, 一个单位正方形可以表示为 [1,1,2,2]. ( ...
- Leetcode 391.完美矩形
完美矩形 我们有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域. 每个矩形用左下角的点和右上角的点的坐标来表示.例如, 一个单位正方形可以表示为 [1,1 ...
- Java实现 LeetCode 391 完美矩形
391. 完美矩形 我们有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域. 每个矩形用左下角的点和右上角的点的坐标来表示.例如, 一个单位正方形可以表示为 ...
- [Swift]LeetCode391. 完美矩形 | Perfect Rectangle
Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...
- [LeetCode] Rectangle Area 矩形面积
Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...
- [LeetCode] Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- Perfect Scrollbar – 完美的 jQuery 滚动条插件
Perfect Scrollbar 是一个很小的,但完美的 jQuery 滚动插件.滚动条不会影响原来的设计布局,滚动条的设计是完全可定制的.你可以改变几乎所有的 CSS 样式的滚动条,滚动条设计对脚 ...
- Leetcode: Perfect Rectangle
Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...
随机推荐
- 题解 P1774 【最接近神的人_NOI导刊2010提高(02)】
这道题很明显是求逆序对. 所谓逆序对,就是逆序的数对. 譬如在下面这个数列中: 1 2 3 4 6 5 6 5就是一个逆序对. 求逆序对的方法比较多,常见的有归并排序和树状数组(线段树当然也行). 本 ...
- nignx 502错误不能使用/的路径方式 即pathinfo
在server中加入 include enable-php-pathinfo.conf; 引入nginx.conf下的这个文件即可. 如果是tp框架,主要隐藏index.php的入口文件,再加入下面这 ...
- tp框架 验证码的应用注意事项
1如何点击更换二维码 二维码是img标签的src访问生成二维码的方法.绑定点击事件,ajax的get方式请求生成二维码的函数.在U函数后面加上任意不重复的参数 如 ?rand=’+math.rand ...
- 【codeforces 738E】Subordinates
[题目链接]:http://codeforces.com/problemset/problem/738/E [题意] 给你一个类似树形的关系; 然后告诉你某个人头顶上有多少个上司numi; 只有fat ...
- 【codeforces 810A】Straight «A»
[题目链接]:http://codeforces.com/contest/810/problem/A [题意] 有n门课的成绩,和一个整数k代表每门课的满分都是k分; 然后这n门课的成绩是按照平均分算 ...
- [SCSS] Pure CSS for multiline truncation with ellipsis
1. Pure CSS 2. Responsive 3. No need to recalculate on resize or font’s load event 4. Cross browser
- MongoDB增加数据
MongoDB中出了增加之外,其他的操作都很麻烦. 例子: 1.简单的 db.infos.insert({"url":"www.baidu.com"}); 2. ...
- m_Orchestrate learning system---八、下拉列表(select标签)如何实现链接功能
m_Orchestrate learning system---八.下拉列表(select标签)如何实现链接功能 一.总结 一句话总结:option的值就是链接地址,选择事件为指向选中的option的 ...
- POJ 3269 中位数
题意: 思路: 这道题坑也不少.. 你准备好脑洞了么? 首先 要认真审题 题目中有说:"没有两头牛的吃草位置是相邻的" 这句话让我们省了很多的事儿 (Discuss里有的大神就入了 ...
- python中index、slice与slice assignment用法
python中index.slice与slice assignment用法 一.index与slice的定义: index用于枚举list中的元素(Indexes enumerate the elem ...